@@ -32,6 +32,26 @@ const long SERIAL_BAUD_RATE = 38400;
3232const uint8_t RUN_CURRENT_PERCENT = 100 ;
3333const uint8_t HOLD_CURRENT_STANDSTILL = 0 ;
3434
35+ // variables
36+ qindesign::network::EthernetUDP udp;
37+ TMC2209 stepper_driver;
38+ DriveEncodersMessage requestMessage;
39+ ArmEffortRequest armEffortRequest;
40+ ArmPositionFeedback armPositionFeedback;
41+
42+ // timer variables
43+ elapsedMillis blinkTimer;
44+ elapsedMillis stepperUpdateTimer;
45+ elapsedMillis motorUpdateTimer;
46+ RoboClaw roboclaw (&Serial2, 38400 );
47+
48+ std::vector<int >::iterator mySpeed;
49+ std::vector<int > stepperSpeeds;
50+
51+ std::unordered_map<int , int > lastCommand;
52+
53+ IPAddress remoteIP;
54+
3555int main () {
3656 // Initialize PortHandler instance
3757 // Set the port path
@@ -201,10 +221,10 @@ int main() {
201221 motorUpdateTimer -= MOTOR_UPDATE_RATE;
202222
203223 // new arm motor effort
204- run_roboclaw_effort (ROBOCLAW_ELBOW_ADDR, ROBOCLAW_CHANNEL_1, armEffortRequest.elbowLiftEffort );
205- run_roboclaw_effort (ROBOCLAW_ELBOW_ADDR, ROBOCLAW_CHANNEL_2, armEffortRequest.shoulderLiftEffort );
206- run_roboclaw_effort (ROBOCLAW_WRIST_ADDR, ROBOCLAW_CHANNEL_1, armEffortRequest.wristSwivelEffort );
207- run_roboclaw_effort (ROBOCLAW_WRIST_ADDR, ROBOCLAW_CHANNEL_2, armEffortRequest.wristLiftEffort );
224+ // run_roboclaw_effort(ROBOCLAW_ELBOW_ADDR, ROBOCLAW_CHANNEL_1, armEffortRequest.elbowLiftEffort);
225+ // run_roboclaw_effort(ROBOCLAW_ELBOW_ADDR, ROBOCLAW_CHANNEL_2, armEffortRequest.shoulderLiftEffort);
226+ // run_roboclaw_effort(ROBOCLAW_WRIST_ADDR, ROBOCLAW_CHANNEL_1, armEffortRequest.wristSwivelEffort);
227+ // run_roboclaw_effort(ROBOCLAW_WRIST_ADDR, ROBOCLAW_CHANNEL_2, armEffortRequest.wristLiftEffort);
208228 run_roboclaw_effort (ROBOCLAW_SHOULDER_ADDR, ROBOCLAW_CHANNEL_1, armEffortRequest.shoulderSwivelEffort );
209229 }
210230
@@ -237,6 +257,74 @@ int main() {
237257 }
238258}
239259
260+ void test_stepper () {
261+ stepper_driver.moveAtVelocity (RUN_VELOCITY);
262+ stepper_driver.disableInverseMotorDirection ();
263+ stepper_driver.enable ();
264+ delay (RUN_DURATION);
265+ stepper_driver.disable ();
266+ delay (STOP_DURATION);
267+ stepper_driver.enableInverseMotorDirection ();
268+ stepper_driver.enable ();
269+ delay (RUN_DURATION);
270+ stepper_driver.disable ();
271+ delay (STOP_DURATION);
272+ }
273+
274+ void test_stepper_2 () {
275+ // stepper_driver.disable();
276+ if (*mySpeed < 0 ) {
277+ stepper_driver.disableInverseMotorDirection ();
278+ } else {
279+ stepper_driver.enableInverseMotorDirection ();
280+ }
281+
282+ int run_speed = abs (*mySpeed) * 10 ;
283+ stepper_driver.moveAtVelocity (run_speed);
284+ stepper_driver.enable ();
285+
286+ mySpeed++;
287+ if (mySpeed == stepperSpeeds.end ()) {
288+ mySpeed = stepperSpeeds.begin ();
289+ }
290+ }
291+
292+ void run_stepper () {
293+ // stepper_driver.disable();
294+ if (requestMessage.leftSpeed < 0 ) {
295+ stepper_driver.disableInverseMotorDirection ();
296+ } else {
297+ stepper_driver.enableInverseMotorDirection ();
298+ }
299+
300+ int run_speed = abs (requestMessage.leftSpeed ) * 50 ;
301+
302+ if (run_speed >= 1000 ) {
303+ stepper_driver.moveAtVelocity (run_speed);
304+ stepper_driver.enable ();
305+ } else {
306+ stepper_driver.disable ();
307+ }
308+ }
309+
310+ void run_stepper_2 () {
311+ // stepper_driver.disable();
312+ if (armEffortRequest.clawVel < 0 ) {
313+ stepper_driver.disableInverseMotorDirection ();
314+ } else {
315+ stepper_driver.enableInverseMotorDirection ();
316+ }
317+
318+ int run_speed = abs (armEffortRequest.clawVel ) * 50 ;
319+
320+ if (run_speed >= 1000 ) {
321+ stepper_driver.moveAtVelocity (run_speed);
322+ stepper_driver.enable ();
323+ } else {
324+ stepper_driver.disable ();
325+ }
326+ }
327+
240328void run_roboclaw_effort (int address, int channel, int effort) {
241329
242330 int hash = address * 10 + channel;
@@ -260,4 +348,18 @@ void run_roboclaw_effort(int address, int channel, int effort) {
260348 roboclaw.ForwardM2 (addr, requestedSpeed);
261349 }
262350 }
351+ }
352+
353+ void run_roboclaw_speed (int address, int channel, int speed) {
354+ int hash = address * 10 + channel;
355+ if (lastCommand.count (hash) > 0 && lastCommand[hash] == speed) return ;
356+ lastCommand[hash] = speed;
357+
358+ uint8_t addr = address;
359+
360+ if (channel == 1 ) {
361+ roboclaw.SpeedM1 (addr, speed);
362+ } else if (channel == 2 ) {
363+ roboclaw.SpeedM2 (addr, speed);
364+ }
263365}
0 commit comments