Skip to content

Commit 134e008

Browse files
committed
simplified part of scienceto just be about the drill
1 parent 0a52172 commit 134e008

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

jetson_script/Science.cpp

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,114 @@
1+
#include <RoboClaw.h>
2+
#include "urc.ph.h"
13

4+
constexpr int DRILL_ENABLE_PIN = 29;
5+
constexpr int DRILL_SIGNAL_1_PIN = 25;
6+
constexpr int DRILL_SIGNAL_2_PIN = 24;
7+
constexpr int ROBOCLAW_DRILL_ADDR = 0x80;
8+
9+
TMC2209 stepperDrill;
10+
RoboClaw roboclaw(&Serial6, 38400);
11+
12+
void run_drill_stepper(int speed) {
13+
if (speed < 0) {
14+
stepperDrill.disableInverseMotorDirection();
15+
} else {
16+
stepperDrill.enableInverseMotorDirection();
17+
}
18+
19+
int run_speed = abs(speed) * 50;
20+
21+
if (run_speed >= 1000) {
22+
stepperDrill.moveAtVelocity(run_speed);
23+
stepperDrill.enable();
24+
} else {
25+
stepperDrill.disable();
26+
}
27+
}
28+
29+
// pin setup
30+
pinMode(LED_BUILTIN, OUTPUT);
31+
pinMode(STEPPER_1_ENABLE_PIN, OUTPUT);
32+
digitalWrite(STEPPER_1_ENABLE_PIN, LOW);
33+
pinMode(STEPPER_2_ENABLE_PIN, OUTPUT);
34+
digitalWrite(STEPPER_2_ENABLE_PIN, LOW);
35+
36+
pinMode(DRILL_ENABLE_PIN, OUTPUT);
37+
pinMode(DRILL_SIGNAL_1_PIN, OUTPUT);
38+
pinMode(DRILL_SIGNAL_2_PIN, OUTPUT);
39+
40+
constexpr char hostName[]{"Science_Teensy"};
41+
qindesign::network::Ethernet.setHostname(hostName);
42+
qindesign::network::Ethernet.begin();
43+
udp.begin(PORT);
44+
scienceMotorRequest = ScienceMotorRequest_init_zero;
45+
uint8_t requestBuffer[256];
46+
size_t requestLength;
47+
48+
// Stepper setup
49+
Serial1.begin(STEPPER_SERIAL_BAUD_RATE);
50+
stepperDrill.setup(Serial1);
51+
stepperDrill.setRunCurrent(RUN_CURRENT_PERCENT);
52+
stepperDrill.setHoldCurrent(HOLD_CURRENT_STANDSTILL);
53+
stepperDrill.enable();
54+
stepperDrill.moveAtVelocity(STOP_VELOCITY);
55+
delay(STOP_DURATION);
56+
stepperDrill.moveAtVelocity(RUN_VELOCITY);
57+
58+
Serial2.begin(STEPPER_SERIAL_BAUD_RATE);
59+
stepperTurntable.setup(Serial2);
60+
stepperTurntable.setRunCurrent(RUN_CURRENT_PERCENT);
61+
stepperTurntable.setHoldCurrent(HOLD_CURRENT_STANDSTILL);
62+
stepperTurntable.enable();
63+
stepperTurntable.moveAtVelocity(STOP_VELOCITY);
64+
delay(STOP_DURATION);
65+
stepperTurntable.moveAtVelocity(RUN_VELOCITY);
66+
67+
// Roboclaw setup
68+
roboclaw.begin(38400);
69+
70+
// test setup
71+
setup_test_stepper();
72+
73+
while (true) {
74+
75+
// receive UDP packets
76+
requestLength = udp.parsePacket();
77+
if (udp.available()) {
78+
79+
memset(requestBuffer, 0, 256);
80+
udp.readBytes(requestBuffer, requestLength);
81+
remoteIP = udp.remoteIP();
82+
protobuf::Messages::decodeRequest(requestBuffer, requestLength, scienceMotorRequest);
83+
84+
Serial.print("Packet received: ");
85+
Serial.print("drillEffort ");
86+
Serial.print(scienceMotorRequest.drillEffort);
87+
Serial.print(" hasDE ");
88+
Serial.print(scienceMotorRequest.has_drillEffort);
89+
Serial.print(" hasLSV ");
90+
Serial.print(scienceMotorRequest.has_leadscrewVel);
91+
Serial.print(" hasTTV ");
92+
Serial.print(scienceMotorRequest.has_turntableVel);
93+
Serial.print(" LSV ");
94+
Serial.print(scienceMotorRequest.leadscrewVel);
95+
Serial.print(" TTV ");
96+
Serial.print(scienceMotorRequest.turntableVel);
97+
Serial.println(" ");
98+
}
99+
100+
// steppers
101+
if (stepperUpdateTimer >= STEPPER_UPDATE_RATE_MS) {
102+
stepperUpdateTimer -= STEPPER_UPDATE_RATE_MS;
103+
104+
run_turntable_stepper(scienceMotorRequest.turntableVel);
105+
run_drill_stepper(scienceMotorRequest.leadscrewVel);
106+
run_roboclaw_effort(ROBOCLAW_DRILL_ADDR, scienceMotorRequest.drillEffort);
107+
}
108+
109+
// blink
110+
if (blinkTimer >= BLINK_RATE_MS) {
111+
blinkTimer -= BLINK_RATE_MS;
112+
digitalToggle(LED_BUILTIN);
113+
}
114+
}

0 commit comments

Comments
 (0)