-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathROSRobotControl.ino
More file actions
250 lines (190 loc) · 6.08 KB
/
ROSRobotControl.ino
File metadata and controls
250 lines (190 loc) · 6.08 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
//
// ROSRobotControl - Implements a ROS node for the Pololu A-Star for controlling
// a ROSRev2-class robot.
//
// Needed on Leonardo to force use of USB serial.
//#define USE_USBCON
#include <AStar32U4.h>
#include <EnableInterrupt.h>
#include <SimplePID.h>
#include <ros.h>
#include <std_msgs/Int16.h>
#include <std_msgs/Int32.h>
#include <std_msgs/Float32.h>
const int ONBOARD_LED_PIN = 13;
// Pins for the A-Star motor encoder outputs.
const int M1_A = 7;
const int M1_B = 11;
const int M2_A = 15;
const int M2_B = 16;
ros::NodeHandle_<ArduinoHardware, 6, 6, 150, 150> nh;
std_msgs::Int16 lwheelMsg;
ros::Publisher lwheelPub("lwheel", &lwheelMsg);
std_msgs::Int16 rwheelMsg;
ros::Publisher rwheelPub("rwheel", &rwheelMsg);
std_msgs::Float32 lwheelVelocityMsg;
ros::Publisher lwheelVelocityPub("lwheel_velocity", &lwheelVelocityMsg);
std_msgs::Float32 rwheelVelocityMsg;
ros::Publisher rwheelVelocityPub("rwheel_velocity", &rwheelVelocityMsg);
void lwheelTargetCallback(const std_msgs::Float32& cmdMsg);
ros::Subscriber<std_msgs::Float32> lwheelTargetSub("lwheel_vtarget", &lwheelTargetCallback);
void rwheelTargetCallback(const std_msgs::Float32& cmdMsg);
ros::Subscriber<std_msgs::Float32> rwheelTargetSub("rwheel_vtarget", &rwheelTargetCallback);
AStar32U4Motors motors;
// Ziegler-Nichols tuning. See this Wikipedia article for details:
// https://en.wikipedia.org/wiki/PID_controller#Loop_tuning
const float Ku = .15;
const float Tu = .1142857143;
const float Kp = 0.6*Ku;
const float Ki = 2*Kp/Tu;
const float Kd = Kp*Tu/8;
SimplePID leftController = SimplePID(Kp, Ki, Kd);
SimplePID rightController = SimplePID(Kp, Ki, Kd);
volatile long lwheel = 0;
volatile long rwheel = 0;
long lastLwheel = 0;
long lastRwheel = 0;
// Target motors speeds in ticks per second.
int lwheelTargetRate = 0;
int rwheelTargetRate = 0;
// The interval between motor control steps.
int controlDelayMillis;
// The number of encoder ticks per meter.
int ticksPerMeter;
// The number of milliseconds without a velocity target when the robot
// will automatically turn off the motors.
int vtargetTimeoutMillis;
unsigned long lastLoopTime;
unsigned long lastMotorCmdTime;
int leftMotorCmd = 0;
int rightMotorCmd = 0;
// Minimum motor control value. Motor output below this will stall.
const int MIN_MOTOR_CMD = 60;
// Maximum motor control value.
const int MAX_MOTOR_CMD = 400;
void setup() {
pinMode(ONBOARD_LED_PIN, OUTPUT);
enableInterrupt(M1_A, leftAChange, CHANGE);
enableInterrupt(M1_B, leftBChange, CHANGE);
enableInterrupt(M2_A, rightAChange, CHANGE);
enableInterrupt(M2_B, rightBChange, CHANGE);
nh.initNode();
nh.advertise(lwheelPub);
nh.advertise(rwheelPub);
nh.advertise(lwheelVelocityPub);
nh.advertise(rwheelVelocityPub);
nh.subscribe(lwheelTargetSub);
nh.subscribe(rwheelTargetSub);
// Wait until the node has initialized before getting parameters.
while(!nh.connected()) {
nh.spinOnce();
}
int controlRate;
if (!nh.getParam("~control_rate", &controlRate)) {
controlRate = 60;
}
controlDelayMillis = 1000.0 / controlRate;
if (!nh.getParam("ticks_meter", &ticksPerMeter)) {
ticksPerMeter = 11931;
}
float vtargetTimeout;
if (!nh.getParam("~vtarget_timeout", &vtargetTimeout)) {
vtargetTimeout = 0.250;
}
vtargetTimeoutMillis = vtargetTimeout * 1000;
lastLoopTime = micros();
lastMotorCmdTime = lastLoopTime;
}
// Every loop, publish the encoder and wheel rates.
void loop()
{
delay(controlDelayMillis);
long curLoopTime = micros();
noInterrupts();
long curLwheel = lwheel;
long curRwheel = rwheel;
interrupts();
lwheelMsg.data = (int) curLwheel;
rwheelMsg.data = (int) curRwheel;
lwheelPub.publish(&lwheelMsg);
rwheelPub.publish(&rwheelMsg);
float dt = (curLoopTime - lastLoopTime) / 1E6;
float lwheelRate = ((curLwheel - lastLwheel) / dt);
float rwheelRate = ((curRwheel - lastRwheel) / dt);
lwheelVelocityMsg.data = lwheelRate / ticksPerMeter;
rwheelVelocityMsg.data = rwheelRate / ticksPerMeter;
lwheelVelocityPub.publish(&lwheelVelocityMsg);
rwheelVelocityPub.publish(&rwheelVelocityMsg);
int leftControl = leftController.getControlValue(lwheelRate, dt);
leftMotorCmd += min(MAX_MOTOR_CMD, leftControl);
leftMotorCmd = constrain(leftMotorCmd, -MAX_MOTOR_CMD, MAX_MOTOR_CMD);
if (leftMotorCmd > 0) {
leftMotorCmd = max(leftMotorCmd, MIN_MOTOR_CMD);
}
int rightControl = rightController.getControlValue(rwheelRate, dt);
rightMotorCmd += min(MAX_MOTOR_CMD, rightControl);
rightMotorCmd = constrain(rightMotorCmd, -MAX_MOTOR_CMD, MAX_MOTOR_CMD);
if (rightMotorCmd > 0) {
rightMotorCmd = max(rightMotorCmd, MIN_MOTOR_CMD);
}
// Coast to a stop if target is zero.
if (lwheelTargetRate == 0) {
leftMotorCmd = 0;
}
if (rwheelTargetRate == 0) {
rightMotorCmd = 0;
}
setSpeed(leftMotorCmd, rightMotorCmd);
// Turn off motors if too much time has elapsed since last motor command.
if (millis() - lastMotorCmdTime > vtargetTimeoutMillis) {
lwheelTargetRate = 0;
rwheelTargetRate = 0;
setSpeed(0, 0);
}
lastLwheel = curLwheel;
lastRwheel = curRwheel;
lastLoopTime = curLoopTime;
nh.spinOnce();
}
void lwheelTargetCallback(const std_msgs::Float32& cmdMsg) {
lastMotorCmdTime = millis();
lwheelTargetRate = cmdMsg.data * ticksPerMeter;
leftController.setSetPoint(lwheelTargetRate);
}
void rwheelTargetCallback(const std_msgs::Float32& cmdMsg) {
lastMotorCmdTime = millis();
rwheelTargetRate = cmdMsg.data * ticksPerMeter;
rightController.setSetPoint(rwheelTargetRate);
}
void leftAChange() {
if (digitalRead(M1_A) == digitalRead(M1_B)) {
++lwheel;
} else {
--lwheel;
}
}
void leftBChange() {
if (digitalRead(M1_A) != digitalRead(M1_B)) {
++lwheel;
} else {
--lwheel;
}
}
void rightAChange() {
if (digitalRead(M2_A) != digitalRead(M2_B)) {
++rwheel;
} else {
--rwheel;
}
}
void rightBChange() {
if (digitalRead(M2_A) == digitalRead(M2_B)) {
++rwheel;
} else {
--rwheel;
}
}
// Sets the left and right motor speeds.
void setSpeed(int leftSpeed, int rightSpeed) {
motors.setSpeeds(leftSpeed, rightSpeed);
}