feat: main loop now listens to state
This commit is contained in:
+35
-24
@@ -75,33 +75,44 @@ void MainLoop::update() noexcept
|
||||
UartHandler& uart = UartHandler::getInstance();
|
||||
uart.poll();
|
||||
|
||||
while(uart.getNumAvailablePackets()) {
|
||||
uint8_t packet[16];
|
||||
uart.getNextPacket(packet);
|
||||
CommandParser::Command command = CommandParser::getInstance().parsePacket(packet);
|
||||
switch (StateMachine::getInstance().getState()) {
|
||||
case StateMachine::STATE::IDLING:
|
||||
case StateMachine::STATE::MOVING:
|
||||
while(uart.getNumAvailablePackets()) {
|
||||
uint8_t packet[16];
|
||||
uart.getNextPacket(packet);
|
||||
CommandParser::Command command = CommandParser::getInstance().parsePacket(packet);
|
||||
|
||||
switch (command.instruction) {
|
||||
// Scoping to keep variables local
|
||||
case CommandParser::Command::TYPE::MOVE_ABSOLUTE: {
|
||||
AdafruitServoMotor& motor = motors[command.args[0]];
|
||||
const float moveTarget = (float)command.args[1] / 10.0f;
|
||||
motor.moveTo(moveTarget);
|
||||
}
|
||||
break;
|
||||
case CommandParser::Command::TYPE::MOVE_RELATIVE: {
|
||||
AdafruitServoMotor& motor = motors[command.args[0]];
|
||||
const float moveDelta = (float)command.args[1] / 10.0f;
|
||||
motor.moveTo(motor.getPosition() + moveDelta);
|
||||
}
|
||||
break;
|
||||
case CommandParser::Command::TYPE::RESET_POSITIONS: {
|
||||
for (uint8_t i = 0; i < 3; i++) {
|
||||
motors[i].moveTo(0);
|
||||
motors[i].commitMoveImmediately();
|
||||
switch (command.instruction) {
|
||||
// Scoping to keep variables local
|
||||
case CommandParser::Command::TYPE::MOVE_ABSOLUTE: {
|
||||
AdafruitServoMotor& motor = motors[command.args[0]];
|
||||
const float moveTarget = (float)command.args[1] / 10.0f;
|
||||
motor.moveTo(moveTarget);
|
||||
}
|
||||
break;
|
||||
case CommandParser::Command::TYPE::MOVE_RELATIVE: {
|
||||
AdafruitServoMotor& motor = motors[command.args[0]];
|
||||
const float moveDelta = (float)command.args[1] / 10.0f;
|
||||
motor.moveTo(motor.getPosition() + moveDelta);
|
||||
}
|
||||
break;
|
||||
case CommandParser::Command::TYPE::RESET_POSITIONS: {
|
||||
for (uint8_t i = 0; i < 3; i++) {
|
||||
motors[i].moveTo(0);
|
||||
motors[i].commitMoveImmediately();
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
break;
|
||||
case StateMachine::STATE::ERROR:
|
||||
default:
|
||||
// Freeze in place until error clears or device reset
|
||||
yield();
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user