Skip to content

Commit

Permalink
ensure accel sends motor pos once done
Browse files Browse the repository at this point in the history
  • Loading branch information
beniroquai committed Dec 15, 2023
1 parent 9ccd7a0 commit 16b8cc9
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 50 deletions.
2 changes: 1 addition & 1 deletion main/json_api_BD.txt
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@
}
}

{"task":"/motor_act","qid":4, "motor":{"steppers": [{ "stepperid": 1, "position": 40000, "speed": -20000, "isabs": 0, "isaccel":1, "accel":10000}]}}
{"task":"/motor_act","qid":4, "motor":{"steppers": [{ "stepperid": 1, "position": 400, "speed": -20000, "isabs": 0, "isaccel":1, "accel":10000}]}}
{"task":"/motor_act","qid":4, "motor":{"steppers": [{ "stepperid": 0, "position": -40000, "speed": -20000, "isabs": 0, "isaccel":1, "accel":10000}]}}
{"task":"/motor_act","qid":4, "motor":{"steppers": [{ "stepperid": 0, "position": 40000, "speed": -20000, "isabs": 0, "isaccel":1, "accel":10000}]}}
{"task":"/motor_act","qid":4, "motor":{"steppers": [{ "stepperid": 0, "position": -20000, "speed": -20000, "isabs": 0, "isaccel":1, "accel":10000}]}}
Expand Down
5 changes: 3 additions & 2 deletions main/src/motor/AccelStep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "ModuleController.h"
#include "FocusMotor.h"
#include "../digitalout/DigitalOutController.h"
#include "Module.h"

void driveMotorXLoop(void *pvParameter)
{
Expand Down Expand Up @@ -123,8 +124,6 @@ void AccelStep::setupAccelStepper()
log_d("setting default values for motor %i", i);
steppers[i]->setMaxSpeed(MAX_VELOCITY_A);
steppers[i]->setAcceleration(DEFAULT_ACCELERATION);
steppers[i]->runToNewPosition(-1000);
steppers[i]->runToNewPosition(1000);
steppers[i]->setCurrentPosition(data[i]->currentPosition);
}
}
Expand Down Expand Up @@ -258,6 +257,8 @@ void AccelStep::driveMotorLoop(int stepperid)
vTaskDelete(NULL);
}



long AccelStep::getCurrentPosition(int i)
{
return steppers[i]->currentPosition();
Expand Down
1 change: 1 addition & 0 deletions main/src/motor/AccelStep.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ class AccelStep
void Enable(bool enable);
void setPosition(Stepper s, int val);
bool isRunning(int i);
void sendMotorPos(int i, int arraypos);
};
96 changes: 49 additions & 47 deletions main/src/motor/FocusMotor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,58 +422,60 @@ void FocusMotor::setup()
// dont use it, it get no longer triggered from modulehandler
void FocusMotor::loop()
{
if (pinConfig.useFastAccelStepper)
// checks if a stepper is still running
for (int i = 0; i < data.size(); i++)
{
// checks if a stepper is still running
for (int i = 0; i < data.size(); i++)
// check if motor is registered
if (!data[i]->isActivated)
continue;
bool isRunning = true;
if (pinConfig.useFastAccelStepper)
isRunning = faccel.isRunning(i);
else
isRunning = accel.isRunning(i);

// should only send a response if there is nothing else is sent
State *state = (State *)moduleController.get(AvailableModules::state);
bool isSending = state->isSending;
/*
// Implement an output trigger for a camera that is triggered if the stage has moved n-steps periodically
if (isRunning and data[i]->triggerPeriod > 0)
{
// check if motor is registered
if (!data[i]->isActivated)
continue;
bool isRunning = faccel.isRunning(i);

// should only send a response if there is nothing else is sent
State *state = (State *)moduleController.get(AvailableModules::state);
bool isSending = state->isSending;
/*
// Implement an output trigger for a camera that is triggered if the stage has moved n-steps periodically
if (isRunning and data[i]->triggerPeriod > 0)
{
Stepper s = static_cast<Stepper>(i);
data[i]->currentPosition = getCurrentPosition(s);
log_i("Current position %i", data[i]->currentPosition);
if ((data[i]->currentPosition - data[i]->offsetTrigger) % data[i]->triggerPeriod == 0)
Stepper s = static_cast<Stepper>(i);
data[i]->currentPosition = getCurrentPosition(s);
log_i("Current position %i", data[i]->currentPosition);
if ((data[i]->currentPosition - data[i]->offsetTrigger) % data[i]->triggerPeriod == 0)
{
data[i]->isTriggered = true;
log_i("Triggering pin %i, current Pos %i, trigger period %i", data[i]->triggerPin, data[i]->currentPosition, data[i]->triggerPeriod);
DigitalOutController *digitalOut = (DigitalOutController *)moduleController.get(AvailableModules::digitalout);
digitalOut->setPin(data[i]->triggerPin, data[i]->isTriggered, 0);
}
else
{
if (data[i]->isTriggered)
{
data[i]->isTriggered = true;
log_i("Triggering pin %i, current Pos %i, trigger period %i", data[i]->triggerPin, data[i]->currentPosition, data[i]->triggerPeriod);
data[i]->isTriggered = false;
log_i("Triggering pin %i", data[i]->triggerPin);
DigitalOutController *digitalOut = (DigitalOutController *)moduleController.get(AvailableModules::digitalout);
digitalOut->setPin(data[i]->triggerPin, data[i]->isTriggered, 0);
}
else
{
if (data[i]->isTriggered)
{
data[i]->isTriggered = false;
log_i("Triggering pin %i", data[i]->triggerPin);
DigitalOutController *digitalOut = (DigitalOutController *)moduleController.get(AvailableModules::digitalout);
digitalOut->setPin(data[i]->triggerPin, data[i]->isTriggered, 0);
}
}
}
*/
if (!isRunning && !data[i]->stopped & !isSending)
{
// Only send the information when the motor is halting
// log_d("Sending motor pos %i", i);
stopStepper(i);
sendMotorPos(i, 0);
preferences.begin("motor-positions", false);
preferences.putLong(("motor" + String(i)).c_str(), data[i]->currentPosition);
log_i("Motor %i position: %i", i, data[i]->currentPosition);
preferences.end();
}
}
}
*/
if (((!isRunning && !data[i]->stopped) or (!pinConfig.useFastAccelStepper and !isRunning and data[i]->wasRunning)) & !isSending)
{
// Only send the information when the motor is halting
// log_d("Sending motor pos %i", i);
stopStepper(i);
sendMotorPos(i, 0);
preferences.begin("motor-positions", false);
preferences.putLong(("motor" + String(i)).c_str(), data[i]->currentPosition);
log_i("Motor %i position: %i", i, data[i]->currentPosition);
preferences.end();
}
data[i]->wasRunning = isRunning;
}
}

Expand Down Expand Up @@ -572,11 +574,11 @@ long FocusMotor::getCurrentPosition(Stepper s)
{
return faccel.getCurrentPosition(s);
}
else
else
{
return accel.getCurrentPosition(s);
}

return 0;
}

Expand Down
1 change: 1 addition & 0 deletions main/src/motor/MotorTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ struct MotorData
int timeLastActive = 0;
bool isEnable = 1; // keeping motor on after job is completed?

bool wasRunning = false; // flag to check if the motor was running in the last loop
bool isActivated = 0;//flag to check if the motor is functional or just there to allocate memory :P
int qid = -1; // for keeping sync with commands sent by the serial and their responses

Expand Down

0 comments on commit 16b8cc9

Please sign in to comment.