1+ // Include relevant libraries from StepperArm C++ file
2+ #include < RoboClaw.h>
3+ #include " urc.ph.h"
14
5+
6+ // Constants relevant to shoulder swivel
7+ constexpr int ROBOCLAW_SHOULDER_ADDR = 0x82 ;
8+ constexpr int ROBOCLAW_CHANNEL_1 = 1 ;
9+ constexpr int ROBOCLAW_CHANNEL_2 = 2 ;
10+
11+ const long SERIAL_BAUD_RATE = 38400 ;
12+ const uint8_t RUN_CURRENT_PERCENT = 100 ;
13+ const uint8_t HOLD_CURRENT_STANDSTILL = 0 ;
14+
15+ const int32_t RUN_VELOCITY = 40000 ;
16+ const int32_t STOP_VELOCITY = 0 ;
17+
18+ const long SERIAL_BAUD_RATE = 38400 ;
19+ const uint8_t RUN_CURRENT_PERCENT = 100 ;
20+ const uint8_t HOLD_CURRENT_STANDSTILL = 0 ;
21+
22+ void run_roboclaw_effort (int address, int channel, int effort) {
23+
24+ int hash = address * 10 + channel;
25+ if (lastCommand.count (hash) > 0 && lastCommand[hash] == effort) return ;
26+ lastCommand[hash] = effort;
27+
28+ uint8_t addr = address;
29+ bool isReversed = (effort < 0 );
30+ uint8_t requestedSpeed = abs (effort);
31+
32+ if (channel == 1 ) {
33+ if (isReversed) {
34+ roboclaw.BackwardM1 (addr, requestedSpeed);
35+ } else {
36+ roboclaw.ForwardM1 (addr, requestedSpeed);
37+ }
38+ } else if (channel == 2 ) {
39+ if (isReversed) {
40+ roboclaw.BackwardM2 (addr, requestedSpeed);
41+ } else {
42+ roboclaw.ForwardM2 (addr, requestedSpeed);
43+ }
44+ }
45+ }
46+
47+ int main () {
48+ pinMode (LED_BUILTIN, OUTPUT);
49+ pinMode (2 , OUTPUT);
50+ pinMode (SERVO_PWM_PIN, OUTPUT);
51+ digitalWrite (2 , LOW);
52+
53+ // Ethernet setup
54+ constexpr char hostName[]{" Arm_Teensy" };
55+ qindesign::network::Ethernet.setHostname (hostName);
56+ qindesign::network::Ethernet.begin ();
57+ udp.begin (PORT);
58+
59+ armEffortRequest = ArmEffortRequest_init_zero;
60+ armPositionFeedback = ArmPositionFeedback_init_zero;
61+ // requestMessage = DriveEncodersMessage_init_zero;
62+ uint8_t requestBuffer[256 ];
63+ size_t requestLength;
64+
65+ roboclaw.begin (38400 );
66+
67+ Serial1.begin (SERIAL_BAUD_RATE);
68+ stepper_driver.setup (Serial1);
69+ stepper_driver.setRunCurrent (RUN_CURRENT_PERCENT);
70+ stepper_driver.setHoldCurrent (HOLD_CURRENT_STANDSTILL);
71+ stepper_driver.enable ();
72+ stepper_driver.moveAtVelocity (STOP_VELOCITY);
73+ delay (STOP_DURATION);
74+ stepper_driver.moveAtVelocity (RUN_VELOCITY);
75+
76+ while (true ) {
77+
78+ // read incoming UDP messages
79+ requestLength = udp.parsePacket ();
80+ if (udp.available ()) {
81+
82+ Serial.print (" Packet received: " );
83+
84+ memset (requestBuffer, 0 , 256 );
85+ udp.readBytes (requestBuffer, requestLength);
86+ // protobuf::Messages::decodeRequest(requestBuffer, requestLength, requestMessage);
87+ protobuf::Messages::decodeRequest (requestBuffer, requestLength, armEffortRequest);
88+
89+ remoteIP = udp.remoteIP ();
90+
91+ // Serial.print("left: ");
92+ // Serial.print(requestMessage.leftSpeed);
93+ }
94+
95+ if (motorUpdateTimer >= MOTOR_UPDATE_RATE) {
96+ motorUpdateTimer -= MOTOR_UPDATE_RATE;
97+
98+ // new arm motor effort
99+ run_roboclaw_effort (ROBOCLAW_ELBOW_ADDR, ROBOCLAW_CHANNEL_1, armEffortRequest.elbowLiftEffort );
100+ run_roboclaw_effort (ROBOCLAW_ELBOW_ADDR, ROBOCLAW_CHANNEL_2, armEffortRequest.shoulderLiftEffort );
101+ run_roboclaw_effort (ROBOCLAW_WRIST_ADDR, ROBOCLAW_CHANNEL_1, armEffortRequest.wristSwivelEffort );
102+ run_roboclaw_effort (ROBOCLAW_WRIST_ADDR, ROBOCLAW_CHANNEL_2, armEffortRequest.wristLiftEffort );
103+ run_roboclaw_effort (ROBOCLAW_SHOULDER_ADDR, ROBOCLAW_CHANNEL_1, armEffortRequest.shoulderSwivelEffort );
104+ }
105+
106+ // servo expects 50Hz signal, where HIGH time is between 500us and 2500us
107+ // 1500us means servo stopped
108+ // 1,000,000us / 50 = 20,000us period
109+ if (servoUpdateTimer >= servo_wait_us) {
110+ // reset timer
111+ servoUpdateTimer -= servo_wait_us;
112+
113+ // high pulse has ended
114+ if (servo_pwm_high) {
115+ servo_wait_us = SERVO_PWM_PERIOD_US - abs (servo_wait_us);
116+ servo_pwm_high = false ;
117+ digitalWrite (SERVO_PWM_PIN, LOW);
118+ }
119+ // low pulse has ended
120+ // update high timer interval from protobuf
121+ else {
122+ servo_wait_us = abs (((1000 * static_cast <int >(armEffortRequest.clawVel )) / 600 ) + SERVO_STOPPED_PULSE_WIDTH);
123+ servo_pwm_high = true ;
124+ digitalWrite (SERVO_PWM_PIN, HIGH);
125+ }
126+ }
127+
128+ if (blinkTimer >= BLINK_RATE_MS) {
129+ blinkTimer -= BLINK_RATE_MS;
130+ digitalToggle (LED_BUILTIN);
131+ }
132+ }
133+ }
0 commit comments