Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 93 additions & 89 deletions focusController.ino → focusController/focusController.ino
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@

#include <SPI.h>
#include <Ethernet.h>
#include <Thread.h>
#include <ThreadController.h>
#include <StaticThreadController.h>
// #include <Thread.h>
// #include <ThreadController.h>
// #include <StaticThreadController.h>

// MAC address and IP address for controller
byte mac[] = {
0xA8, 0x61, 0x0A, 0xAE, 0x25, 0x13
};
byte mac[] = {0xA8, 0x61, 0x0A, 0xAE, 0x25, 0x13};
IPAddress ip(192, 168, 1, 11);

// Initialize the Ethernet server library
Expand All @@ -36,7 +34,7 @@ IPAddress ip(192, 168, 1, 11);
EthernetServer server(80);

// Multithread for handling motor movement in background
Thread moveMotor = Thread();
// Thread moveMotor = Thread();

// Focuser fields
volatile int position = 0;
Expand Down Expand Up @@ -82,29 +80,69 @@ void setup() {
Serial.print("Server starting at ");
Serial.println(Ethernet.localIP());

//initialize thread
moveMotor.onRun(startMovement);
moveMotor.enabled = true;
// initialize thread
// moveMotor.onRun(startMovement);
// moveMotor.enabled = true;
}

// Code for formatting json return strings
void sendJSONResponse(EthernetClient client, int code, String body) {
client.println("HTTP/1.1 " + String(code) + " OK");
client.println("Content-Type: application/json");
client.println("Connection: close");
client.println();
client.println("{" + body + "}");
}

void loop() {
// listen for incoming clients
updatePosition();
EthernetClient client = server.available();
if (client) {
// Emulating the potentiometer position
void updatePosition() {
if (moving && (millis() - lastStepTime > stepDelay)) {
position += (target_steps > 0) ? 1 : -1;
target_steps -= (target_steps > 0) ? 1 : -1;
lastStepTime = millis();

if (target_steps == 0 || checkLimits()) {
moving = false;
position = constrain(position, down_limit, up_limit);
}
}
}

handleClient(client);
bool checkLimits() {
return (position >= up_limit) || (position <= down_limit);
}

// give the web browser time to receive the data
delay(1);
void startMovement() {
//Disable thread so another movement call won't overwrite the current movement
moving = true;

// close the connection:
client.stop();
//Serial.println("client disconnected");
// Period is 1/f, we want to convert to ms then divide by 2 since pulseTime should be half of a clock cycle
long pulseTime = long(float(1)*500000.0/clkFreq);

int i = 0;
while (i < moveSteps && !abortMovement) {
long pastTime = micros();
digitalWrite(clkPin, HIGH);
Serial.print("clkHigh");

// delay until halfway through period designated by clk_frequency
while (micros() - pastTime < pulseTime) {}

pastTime = micros();
digitalWrite(clkPin, LOW);
Serial.print("clkLow");

// delay until reach the end of period
while (micros() - pastTime < pulseTime) {}

i++;
}
}
Serial.println();
Serial.println("Movement Finished");

moving = false;
// moveMotor.enabled = true;
}

void handleClient(EthernetClient client) {
char request[128];
Expand All @@ -129,25 +167,35 @@ void handleClient(EthernetClient client) {
*/

/*
Command: move?steps
Paramters: steps: <int32>
Returns: {code: <int32>}
^ Descriptions of each json field are described in Focuser Arduino Specification Google Doc
Test code: "curl -X POST http://192.168.1.11/move?steps=500"
Command: move?steps
Paramters: steps: <int32>
Returns: {code: <int32>}
^ Descriptions of each json field are described in Focuser Arduino Specification Google Doc
Test code: "curl -X POST http://192.168.1.11/move?steps=500"
*/


if (strstr(request, "POST /move?steps")) {
char* stepsParam = strstr(request, "steps=");
if (stepsParam && moveMotor.shouldRun()) {
moveSteps = atoi(stepsParam + 6);
if (stepsParam) { // moveMotor.shouldRun()
// if the request contains '-', then switch to negative direction
char* negative = strstr(request, "-");
if (negative > stepsParam) {
digitalWrite(8, LOW); // is this the right value to write?
moveSteps = atoi(stepsParam + 7); // jump to the value after - sign
}
else {
digitalWrite(8, HIGH);
moveSteps = atoi(stepsParam + 6); // jump to the value after = sign
}

// now tell them we are moving
Serial.print("moving ");
Serial.println(moveSteps);

moveMotor.run();
}
// moveMotor.run();
startMovement();
sendJSONResponse(client, 200, "\"code\":200");
}

/*
Command: status
Paramters: None
Expand Down Expand Up @@ -182,66 +230,22 @@ void handleClient(EthernetClient client) {

sendJSONResponse(client, 300, "\"code\":300");
}
}
}

// Code for formatting json return strings
void sendJSONResponse(EthernetClient client, int code, String body) {
client.println("HTTP/1.1 " + String(code) + " OK");
client.println("Content-Type: application/json");
client.println("Connection: close");
client.println();
client.println("{" + body + "}");
}

// Emulating the potentiometer position
void updatePosition() {
if (moving && (millis() - lastStepTime > stepDelay)) {
position += (target_steps > 0) ? 1 : -1;
target_steps -= (target_steps > 0) ? 1 : -1;
lastStepTime = millis();

if (target_steps == 0 || checkLimits()) {
moving = false;
position = constrain(position, down_limit, up_limit);
}
}
}

bool checkLimits() {
return (position >= up_limit) || (position <= down_limit);
}

void startMovement() {

//Disable thread so another movement call won't overwrite the current movement
moveMotor.enabled = false;
moving = true;

// Period is 1/f, we want to convert to ms then divide by 2 since pulseTime should be half of a clock cycle
long pulseTime = long(float(1)*500000.0/clkFreq);

int i = 0;
while (i < moveSteps && !abortMovement) {
long pastTime = micros();
digitalWrite(clkPin, HIGH);
Serial.print("clkHigh");
void loop() {
// listen for incoming clients
updatePosition();
EthernetClient client = server.available();
if (client) {

// delay until halfway through period designated by clk_frequency
while (micros() - pastTime < pulseTime) {}

pastTime = micros();
digitalWrite(clkPin, LOW);
Serial.print("clkLow");
handleClient(client);

// delay until reach the end of period
while (micros() - pastTime < pulseTime) {}
// give the web browser time to receive the data
delay(1);

i++;
// close the connection:
client.stop();
// Serial.println("client disconnected");
}
Serial.println();
Serial.println("Movement Finished");

moving = false;
moveMotor.enabled = true;
}

Loading