UMRT Arm Firmware Library
arduino_stepper_controller.hpp
1 //
2 // Created by Noah Reeder on 2024-05-30
3 // Based off of ArduinoTest from openFrameworksArduino (https://github.com/NeuroRoboticTech/openFrameworksArduino/blob/master/examples/ArduinoTest.h)
4 //
5 
6 #ifndef UMRT_ARM_FIRMWARE_LIB_ARDUINO_STEPPER_CONTROLLER_HPP
7 #define UMRT_ARM_FIRMWARE_LIB_ARDUINO_STEPPER_CONTROLLER_HPP
8 
9 #include <boost/signals2.hpp>
10 #include <openFrameworksArduino/StdAfx.h>
11 #include <openFrameworksArduino/ofArduino.h>
12 #include <vector>
13 
19 public:
24 
28  ~ArduinoStepperController() noexcept override;
29 
37  bool sendEcho(const std::vector<uint8_t>& payload);
38 
47  bool setSpeed(const uint8_t motor, const int16_t speed);
48 
56  // TODO: Would be nice to include timestamp in response since it is asynchronous
57  bool getSpeed(const uint8_t motor);
58 
69  bool sendStep(const uint8_t motor, const uint16_t num_steps, const int16_t speed);
70 
80  bool seekPosition(const uint8_t motor, const int32_t position, const int16_t speed);
81 
88  bool getPosition(const uint8_t motor);
89 
97  bool setGripper(const uint8_t position);
98 
103  [[nodiscard]] bool isSetup() const;
104 
105  // ==========================
106  // Events
107  // ==========================
108 
113  boost::signals2::signal<void(void)> ESetup;
114 
119  boost::signals2::signal<void(std::vector<uint8_t>)> EArduinoEcho;
120 
125  boost::signals2::signal<void(uint8_t, int16_t)> ESetSpeed;
126 
131  boost::signals2::signal<void(uint8_t, int16_t)> EGetSpeed;
132 
137  boost::signals2::signal<void(uint8_t, uint16_t, int16_t)> ESendStep;
138 
143  boost::signals2::signal<void(uint8_t, int32_t, int16_t)> ESeekPosition;
144 
149  boost::signals2::signal<void(uint8_t, int32_t)> EGetPosition;
150 
155  boost::signals2::signal<void(uint8_t)> ESetGripper;
156 
157 protected:
162  boost::signals2::connection connectionInitialized;
163 
169  void setupArduino(const int& version);
170 
171  // Note that using extended command IDs (i.e. command byte 0x00 followed by a 2 byte command ID) yields undefined behaviour
172  // TODO: We therefore shouldn't be using 0x00 for ARDUINO_ECHO
178  void handleSysex(const std::vector<unsigned char>& message);
179 
188  void handleEArduinoEcho(const std::vector<unsigned char>& message);
189 
190  void handleESetSpeed(const std::vector<unsigned char>& message);
191 
192  void handleEGetSpeed(const std::vector<unsigned char>& message);
193 
194  void handleESendStep(const std::vector<unsigned char>& message);
195 
196  void handleESeekPosition(const std::vector<unsigned char>& message);
197 
198  void handleEGetPosition(const std::vector<unsigned char>& message);
199 
200  void handleESetGripper(const std::vector<unsigned char>& message);
202 
203 private:
207  bool setup_completed;
208 };
209 
210 #endif //UMRT_ARM_FIRMWARE_LIB_ARDUINO_STEPPER_CONTROLLER_HPP
Manages the Firmata connection to an Arduino running the Stepper Controller program.
Definition: arduino_stepper_controller.hpp:18
bool seekPosition(const uint8_t motor, const int32_t position, const int16_t speed)
Sends a SysexCommands::SEEK_POS command to move a motor to specific step position.
Definition: arduino_stepper_controller.cpp:83
bool getSpeed(const uint8_t motor)
Sends a SysexCommands::GET_SPEED command to query the speed of a motor.
Definition: arduino_stepper_controller.cpp:62
~ArduinoStepperController() noexcept override
Destroys an ArduinoStepperController.
Definition: arduino_stepper_controller.cpp:26
bool isSetup() const
Returns whether the connection to the stepper controller Arduino has been fully established.
Definition: arduino_stepper_controller.cpp:112
bool setSpeed(const uint8_t motor, const int16_t speed)
Sends a SysexCommands::SET_SPEED command to set the speed of a motor.
Definition: arduino_stepper_controller.cpp:50
boost::signals2::signal< void(uint8_t, int32_t, int16_t)> ESeekPosition
Boost signal triggered when seekPosition responses are received.
Definition: arduino_stepper_controller.hpp:143
bool getPosition(const uint8_t motor)
Sends a SysexCommands::GET_POS command to query the current position of a motor.
Definition: arduino_stepper_controller.cpp:96
void handleSysex(const std::vector< unsigned char > &message)
Handles System-Exclusive (Sysex) messages received on the Firmata link with the Stepper Controller Ar...
Definition: arduino_stepper_controller.cpp:175
bool setGripper(const uint8_t position)
Sends a SysexCommands::SET_GRIPPER command to set the target position of the gripper servo.
Definition: arduino_stepper_controller.cpp:104
bool sendStep(const uint8_t motor, const uint16_t num_steps, const int16_t speed)
Sends a SysexCommands::SEND_STEP command to move a motor a fixed number of steps.
Definition: arduino_stepper_controller.cpp:70
boost::signals2::signal< void(uint8_t, uint16_t, int16_t)> ESendStep
Boost signal triggered when sendStep responses are received.
Definition: arduino_stepper_controller.hpp:137
boost::signals2::connection connectionInitialized
Boost signal triggered when the initial Firmata connection is established.
Definition: arduino_stepper_controller.hpp:162
boost::signals2::signal< void(uint8_t)> ESetGripper
Boost signal triggered when setGripper responses are received.
Definition: arduino_stepper_controller.hpp:155
boost::signals2::signal< void(void)> ESetup
Boost signal triggered once this ArduinoStepperController is fully setup.
Definition: arduino_stepper_controller.hpp:113
boost::signals2::signal< void(uint8_t, int32_t)> EGetPosition
Boost signal triggered when getPosition responses are received.
Definition: arduino_stepper_controller.hpp:149
boost::signals2::signal< void(std::vector< uint8_t >)> EArduinoEcho
Boost signal triggered when sendEcho responses are received.
Definition: arduino_stepper_controller.hpp:119
void setupArduino(const int &version)
Completes configuration of the Stepper Controller Arduino once a Firmata link has been established.
Definition: arduino_stepper_controller.cpp:30
boost::signals2::signal< void(uint8_t, int16_t)> EGetSpeed
Boost signal triggered when getSpeed responses are received.
Definition: arduino_stepper_controller.hpp:131
bool sendEcho(const std::vector< uint8_t > &payload)
Sends a SysexCommands::ARDUINO_ECHO command with the provided payload.
Definition: arduino_stepper_controller.cpp:42
ArduinoStepperController()
Initializes an ArduinoStepperController.
Definition: arduino_stepper_controller.cpp:14
boost::signals2::signal< void(uint8_t, int16_t)> ESetSpeed
Boost signal triggered when setSpeed responses are received.
Definition: arduino_stepper_controller.hpp:125