-
Notifications
You must be signed in to change notification settings - Fork 30
/
navbot.ino
243 lines (191 loc) · 7.26 KB
/
navbot.ino
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
//AGV Machine - Vinay Lanka
//Import Motor - Cytron SPG30E-30K
#include "Motor.h"
#include <ros.h>
#include <geometry_msgs/Twist.h>
#include<PID_v1.h>
#include <geometry_msgs/Vector3Stamped.h>
#include <ros/time.h>
ros::NodeHandle nh;
#define LOOPTIME 10
Motor right(11,10,20,21);
Motor left(9,8,18,19);
volatile long encoder0Pos = 0; // encoder 1
volatile long encoder1Pos = 0; // encoder 2
double left_kp = 3.8 , left_ki = 0 , left_kd = 0.0; // modify for optimal performance
double right_kp = 4 , right_ki = 0 , right_kd = 0.0;
double right_input = 0, right_output = 0, right_setpoint = 0;
PID rightPID(&right_input, &right_output, &right_setpoint, right_kp, right_ki, right_kd, DIRECT);
double left_input = 0, left_output = 0, left_setpoint = 0;
PID leftPID(&left_input, &left_output, &left_setpoint, left_kp, left_ki, left_kd, DIRECT);
float demandx=0;
float demandz=0;
double demand_speed_left;
double demand_speed_right;
unsigned long currentMillis;
unsigned long prevMillis;
float encoder0Diff;
float encoder1Diff;
float encoder0Error;
float encoder1Error;
float encoder0Prev;
float encoder1Prev;
void cmd_vel_cb( const geometry_msgs::Twist& twist){
demandx = twist.linear.x;
demandz = twist.angular.z;
}
ros::Subscriber<geometry_msgs::Twist> sub("cmd_vel", cmd_vel_cb );
geometry_msgs::Vector3Stamped speed_msg; //create a "speed_msg" ROS message
ros::Publisher speed_pub("speed", &speed_msg); //create a publisher to ROS topic "speed" using the "speed_msg" type
double speed_act_left = 0; //Actual speed for left wheel in m/s
double speed_act_right = 0; //Command speed for left wheel in m/s
void setup() {
nh.initNode();
nh.subscribe(sub);
nh.advertise(speed_pub); //prepare to publish speed in ROS topic
// Serial.begin(115200);
rightPID.SetMode(AUTOMATIC);
rightPID.SetSampleTime(1);
rightPID.SetOutputLimits(-100, 100);
leftPID.SetMode(AUTOMATIC);
leftPID.SetSampleTime(1);
leftPID.SetOutputLimits(-100, 100);
// Serial.println("Basic Encoder Test:");
attachInterrupt(digitalPinToInterrupt(left.en_a), change_left_a, CHANGE);
attachInterrupt(digitalPinToInterrupt(left.en_b), change_left_b, CHANGE);
attachInterrupt(digitalPinToInterrupt(right.en_a), change_right_a, CHANGE);
attachInterrupt(digitalPinToInterrupt(right.en_b), change_right_b, CHANGE);
}
void loop() {
currentMillis = millis();
if (currentMillis - prevMillis >= LOOPTIME){
prevMillis = currentMillis;
demand_speed_left = demandx - (demandz*0.1075);
demand_speed_right = demandx + (demandz*0.1075);
/*PID controller for speed control
Base speed being 1 ms and the demand_speed variables controlling it at fractions of the base.
The PID controller keeps trying to match the difference
in encoder counts to match with the required amount, hence controlling the speed. */
encoder0Diff = encoder0Pos - encoder0Prev; // Get difference between ticks to compute speed
encoder1Diff = encoder1Pos - encoder1Prev;
speed_act_left = encoder0Diff/39.65;
speed_act_right = encoder1Diff/39.65;
encoder0Error = (demand_speed_left*39.65)-encoder0Diff; // 3965 ticks in 1m = 39.65 ticks in 10ms, due to the 10 millis loop
encoder1Error = (demand_speed_right*39.65)-encoder1Diff;
encoder0Prev = encoder0Pos; // Saving values
encoder1Prev = encoder1Pos;
left_setpoint = demand_speed_left*39.65; //Setting required speed as a mul/frac of 1 m/s
right_setpoint = demand_speed_right*39.65;
left_input = encoder0Diff; //Input to PID controller is the current difference
right_input = encoder1Diff;
leftPID.Compute();
left.rotate(left_output);
rightPID.Compute();
right.rotate(right_output);
// Serial.print(encoder0Pos);
// Serial.print(",");
// Serial.println(encoder1Pos);
}
publishSpeed(LOOPTIME);
nh.spinOnce();
}
//Publish function for odometry, uses a vector type message to send the data (message type is not meant for that but that's easier than creating a specific message type)
void publishSpeed(double time) {
speed_msg.header.stamp = nh.now(); //timestamp for odometry data
speed_msg.vector.x = speed_act_left; //left wheel speed (in m/s)
speed_msg.vector.y = speed_act_right; //right wheel speed (in m/s)
speed_msg.vector.z = time/1000; //looptime, should be the same as specified in LOOPTIME (in s)
speed_pub.publish(&speed_msg);
// nh.loginfo("Publishing odometry");
}
// ************** encoders interrupts **************
// ************** encoder 1 *********************
void change_left_a(){
// look for a low-to-high on channel A
if (digitalRead(left.en_a) == HIGH) {
// check channel B to see which way encoder is turning
if (digitalRead(left.en_b) == LOW) {
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
else // must be a high-to-low edge on channel A
{
// check channel B to see which way encoder is turning
if (digitalRead(left.en_b) == HIGH) {
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
}
void change_left_b(){
// look for a low-to-high on channel B
if (digitalRead(left.en_b) == HIGH) {
// check channel A to see which way encoder is turning
if (digitalRead(left.en_a) == HIGH) {
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
// Look for a high-to-low on channel B
else {
// check channel B to see which way encoder is turning
if (digitalRead(left.en_a) == LOW) {
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
}
// ************** encoder 2 *********************
void change_right_a(){
// look for a low-to-high on channel A
if (digitalRead(right.en_a) == HIGH) {
// check channel B to see which way encoder is turning
if (digitalRead(right.en_b) == LOW) {
encoder1Pos = encoder1Pos - 1; // CW
}
else {
encoder1Pos = encoder1Pos + 1; // CCW
}
}
else // must be a high-to-low edge on channel A
{
// check channel B to see which way encoder is turning
if (digitalRead(right.en_b) == HIGH) {
encoder1Pos = encoder1Pos - 1; // CW
}
else {
encoder1Pos = encoder1Pos + 1; // CCW
}
}
}
void change_right_b(){
// look for a low-to-high on channel B
if (digitalRead(right.en_b) == HIGH) {
// check channel A to see which way encoder is turning
if (digitalRead(right.en_a) == HIGH) {
encoder1Pos = encoder1Pos - 1; // CW
}
else {
encoder1Pos = encoder1Pos + 1; // CCW
}
}
// Look for a high-to-low on channel B
else {
// check channel B to see which way encoder is turning
if (digitalRead(right.en_a) == LOW) {
encoder1Pos = encoder1Pos - 1; // CW
}
else {
encoder1Pos = encoder1Pos + 1; // CCW
}
}
}