forked from RMI-NITT/roBoat-resQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quad symmetric locomotion
253 lines (241 loc) · 4.34 KB
/
quad symmetric locomotion
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
250
251
252
253
/*
* 1 ----> right motor ----> e - 8, 22,23-->min85
* 2 ----> back motor ----> 9, 24, 25-->min85
* 3 ----> left motor ----> 10, 26, 27-->min90
* 4 ----> front motor ----> 11, 28, 29-->min95
*/
#include <ultdist.h>
#include <DCMotor.h>
dcm m1(8,22,23);
dcm m2(9,24,25);
dcm m3(10,26,27);
dcm m4(11,28,29);
int vrmin=0, vbmin=0, vlmin=0, vfmin=0; // calibrate and update
int vr=0, vb=0, vl=0, vf=0,rotf=0, error,kp,vmin=100;
int m1s,m2s,m3s,m4s;
int r1,r2,b1,b2,l1,l2,f1,f2;
/*ultdist u3(33,32);
ultdist u2(35,34);
ultdist u1(37,36);
ultdist u0(39,38);
int d1,d2,d3,d4,i=0;*/
int head,bearing=127,j,i,g,sm,face;
int h1[4],h2[4];
int theta = 0; // for now; have to change later
void read_head()
{
if (Serial1.available())
{
head = Serial1.read();
head = head*1.40625;
head=360-head;
}
for(i=0;i<4;i++)
h1[i]=head%90 + i*90 ;
j=0;
while(h1[j]!=head)
{
j++;
}
h2[0]=h1[j];
if(j==3)
{
h2[1]=h1[0];
h2[2]=h1[1];
h2[3]=h1[2];
g=0;
sm=1;
}
else if(j==2)
{
h2[1]=h1[3];
h2[2]=h1[0];
h2[3]=h1[1];
g=1;
sm=2;
}
else if(j==1)
{
h2[1]=h1[2];
h2[2]=h1[3];
h2[3]=h1[0];
g=2;
sm=3;
}
else if(j==0)
{
h2[1]=h1[1];
h2[2]=h1[2];
h2[3]=h1[3];
g=3;
sm=0;
}
}
void checkbearing()
{
int q,v;
for(q=0;q<3;q++)
{
if((h2[q]<bearing)&&(bearing<h2[q+1]))
break;
}
if(q==3)
v=0;
else v=q+1;
if(q==g)
{
if((bearing<=90))
{
if((360-h2[q]+bearing)<45){
//clockwise
face=q;
rotf=-1;
error=abs(360-h2[q]+bearing);
Serial.print("A");
}
else{
//anti
face = v;
rotf=1;
error=abs(90-(360-h2[q]+bearing));
//Serial.print("B");
}
}
else if(bearing>=270){
if((bearing - h2[q])<45){
//clock
rotf=-1;
face=q;
error=abs(bearing - h2[q]);
//Serial.print("C");
}
else{
//anti
error=abs(90+bearing-h2[v]);
rotf=1;
face = v;
//Serial.print("D");
}
}
}
else {
if(bearing>(h2[q]+45) && bearing<h2[v])
{
face = v;
rotf=1;
error=abs(h2[v]-bearing);
//Serial.print("E");
//anticlock;
}
else if(bearing>h2[q] && bearing<h2[v]-45)
{
face = q;
rotf=-1;;
error=abs(h2[q]-bearing);
//Serial.print("F");
//clock;
}
}
/*Serial.print(" ");
Serial.print(q);
Serial.print(v);*/
}
void straight()
{
if(face==3)
{
m1.mspeed(255);
m4.mspeed(255);
m2.mspeed(-255);
m3.mspeed(-255);
}
else if(face==2)
{
m2.mspeed(255);
m1.mspeed(255);
m3.mspeed(-255);
m4.mspeed(-255);
}
else if(face==1)
{
m2.mspeed(255);
m3.mspeed(255);
m4.mspeed(-255);
m1.mspeed(-255);
}
else if(face==0)
{
m4.mspeed(255);
m3.mspeed(255);
m1.mspeed(-255);
m2.mspeed(-255);
}
}
void controlhead()
{
kp=((250-vmin)/45);
if(abs(bearing-h2[face])>4)
{
m1.mspeed(rotf*(error*kp + vmin));
m2.mspeed(rotf*(error*kp + vmin));
m3.mspeed(rotf*(error*kp + vmin));
m3.mspeed(rotf*(error*kp + vmin));
}
else
{
straight();
}
}
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
m1.minit();
m2.minit();
m3.minit();
m4.minit();
/*u1.ultinit();
u2.ultinit();
u3.ultinit();
u4.ultinit();
pinMode(40,OUTPUT);
pinMode(41,OUTPUT);
pinMode(42,OUTPUT);
pinMode(43,OUTPUT);*/
//pinMode(2,INPUT);
}
void loop()
{
//m4.mspeed(90);
//theta++;
/*if(i%4==0) d1=u1.getdist();
else if(i%4==1) d2=u2.getdist();
else if(i%4==2) d3=u3.getdist();
else d4=u4.getdist();
i++;*/
read_head();
checkbearing();
controlhead();
/*Serial.print(face);
Serial.print(" ");
Serial.print(error);
Serial.print(" ");
Serial.print(rotf);
Serial.println(" ");*/
/*Serial.print(h2[0]);
Serial.print(" ");
Serial.print(h2[1]);
Serial.print(" ");
Serial.print(h2[2]);
Serial.print(" ");
Serial.println(h2[3]);*/
/*read_head();
pidrot(0);
Serial.println(head1);*/
/*if(theta==359){
theta=0;
}*/
/*if(digitalRead(2)!=1){
//Serial.println("HELP");
}*/
}