-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhardware.cpp
104 lines (85 loc) · 2.28 KB
/
hardware.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "config.h"
#include "src/AccelStepper/AccelStepper.h"
#include "src/AccelStepper/MultiStepper.h"
#ifdef RF_CONTROL
#include "src/SoftRcPulseOut/SoftRcPulseOut.h"
SoftRcPulseOut penServo;
#else
#include <Servo.h>
Servo penServo;
#endif
AccelStepper rightMotor(AccelStepper::HALF4WIRE, R_stepper_pins[0], R_stepper_pins[2], R_stepper_pins[1], R_stepper_pins[3]);
AccelStepper leftMotor(AccelStepper::HALF4WIRE, L_stepper_pins[0], L_stepper_pins[2], L_stepper_pins[1], L_stepper_pins[3]);
MultiStepper motors;
void setupEngines() {
penServo.attach(servoPin);
leftMotor.setMaxSpeed(max_speed);
rightMotor.setMaxSpeed(max_speed);
motors.addStepper(leftMotor);
motors.addStepper(rightMotor);
}
int step(float distance) {
return int(distance * steps_rev / (wheel_dia * PI));
}
float distance(float degrees) {
return wheel_base * PI * (degrees / 360.0);
}
void move(long step_left, long step_right) {
long steps[2];
steps[0] = step_left;
steps[1] = step_right;
motors.moveTo(steps);
motors.runSpeedToPosition();
leftMotor.setCurrentPosition(0);
rightMotor.setCurrentPosition(0);
}
void forward(float distance) {
Serial.print("↑ "); Serial.print(distance); Serial.println("mm");
int s = step(distance);
move(-s, s);
}
void backward(float distance) {
Serial.print("↓ "); Serial.print(distance); Serial.println("mm");
int s = step(distance);
move(s, -s);
}
void right(float degrees) {
Serial.print("↷ "); Serial.print(degrees); Serial.println("°");
int s = step(distance(degrees));
move(s, s);
}
void left(float degrees) {
Serial.print("↶ "); Serial.print(degrees); Serial.println("°");
int s = step(distance(degrees));
move(-s, -s);
}
void done() { // unlock stepper to save battery
rightMotor.disableOutputs();
leftMotor.disableOutputs();
}
void penup() {
Serial.println("⇞");
#ifdef RF_CONTROL
for (int pos = PEN_DOWN; pos > PEN_UP - 15; pos -= 1) {
penServo.write(pos);
delay(2);
SoftRcPulseOut::refresh();
}
#else
penServo.write(PEN_UP);
#endif
delay(100);
}
void pendown() {
Serial.println("⇟");
#ifdef RF_CONTROL
for (int pos = PEN_UP; pos < PEN_DOWN + 15; pos += 1) {
penServo.write(pos);
delay(2);
SoftRcPulseOut::refresh();
}
#else
penServo.write(PEN_DOWN);
#endif
delay(100);
}