1
1
#include < ps2. h>
2
2
3
- // Serial communication vars
4
3
char cmd[20 ];
5
- int index;
4
+ int index = 0 ;
6
5
7
- int Setpoint1 , Input1 , Output1 ;
8
- int Setpoint2 , Input2 , Output2 ;
6
+ int Setpoint1 = 0 ;
7
+ int Input1 = 0 ;
8
+ int Output1 = 0 ;
9
+ int Setpoint2 = 0 ;
10
+ int Input2 = 0 ;
11
+ int Output2 = 0 ;
9
12
10
- // Motor's pins
11
13
int motorApinA = 3 ;
12
14
int motorApinB = 5 ;
13
15
int motorBpinA = 9 ;
14
16
int motorBpinB = 11 ;
15
17
18
+ int spdLeft = 0 ;
19
+ int spdRight = 0 ;
20
+ int spdUp = 0 ;
21
+ int spdDown = 0 ;
22
+
16
23
// Mouse initialisation. PS2Mouse(clock, data)
17
24
PS2Mouse mouse(12 , 7 );
18
25
PS2Mouse mouse2(8 , 2 );
19
26
20
- boolean goTo1;
21
- boolean goTo2;
22
- boolean sendPosition;
27
+ boolean goTo1 = false ;
28
+ boolean goTo2 = false ;
29
+ boolean sendPosition = false ;
30
+ boolean moveRight = false ;
31
+ boolean moveLeft = false ;
32
+ boolean moveUp = false ;
33
+ boolean moveDown = false ;
23
34
24
35
MouseInfo mouseInfo;
25
36
MouseInfo mouseInfo2;
26
37
27
- int xPosition;
28
- int yPosition;
29
- int previousPosition1;
30
- int previousPosition2;
31
- int tmp1;
32
- int tmp2;
33
- int spd1;
34
- int spd2;
38
+ int xPosition = 0 ;
39
+ int yPosition = 0 ;
40
+ int previousPosition1 = 0 ;
41
+ int previousPosition2 = 0 ;
42
+ int tmp1 = 0 ;
43
+ int tmp2 = 0 ;
44
+ int spd1 = 0 ;
45
+ int spd2 = 0 ;
35
46
36
47
void setup ()
37
48
{
49
+ Serial . begin(115200 );
50
+
38
51
pinMode(motorApinA, OUTPUT );
39
52
pinMode(motorApinB, OUTPUT );
40
53
pinMode(motorBpinA, OUTPUT );
41
54
pinMode(motorBpinB, OUTPUT );
42
55
43
- Serial . begin(9600 );
44
-
45
- xPosition = 0 ;
46
- yPosition = 0 ;
47
-
48
- Input1 = 0 ;
49
- Setpoint1 = 0 ;
50
- Input2 = 0 ;
51
- Setpoint2 = 0 ;
56
+ delay (2000 );// wait for mouse to open up
52
57
53
58
mouse. init();
54
59
mouse2. init();
55
60
56
- goTo1 = false ;
57
- goTo2 = false ;
58
- sendPosition = false ;
59
61
}
60
-
61
62
void loop ()
62
63
{
64
+ while (Serial . available())
65
+ {
66
+ cmd[index] = Serial . read();
67
+ delay (1 );
68
+ index++ ;
69
+ }
70
+ cmd[index] = ' \0 ' ;
71
+
72
+
63
73
/** ********************** Mouse routines **************************/
64
74
mouse. getData(& mouseInfo);
65
75
mouse2. getData(& mouseInfo2);
66
76
xPosition = mouseInfo. cX;
67
- yPosition = mouseInfo2. cY ;
77
+ yPosition = mouseInfo2. cX ;
68
78
/** ****************************************************************/
69
-
70
- /** ******************* Serial Communication ***********************/
71
- index = 0 ;
72
- // Get the serial command and store it to the char array
73
- while (Serial . available())
74
- {
75
- cmd[index] = Serial . read();
76
- delay (1 );
77
- index++ ;
78
- }
79
- cmd[index] = 0 ;
80
-
81
79
if (index > 0 )
82
- {
80
+ {
81
+ // Goes to specific set points: G100,200
83
82
if (cmd[0 ] == ' G' ){
84
83
Setpoint2 = atoi(strtok((cmd+ 1 ), " ," ));
85
84
Setpoint1 = atoi(strtok(NULL , " ," ));
86
-
85
+
87
86
previousPosition1 = yPosition;
88
87
previousPosition2 = xPosition;
89
88
goTo1 = true ;
90
89
goTo2 = true ;
91
90
}
91
+ // Stops the motors
92
92
if (cmd[0 ] == ' S' )
93
93
{
94
94
goTo1 = false ;
95
95
goTo2 = false ;
96
96
sendPosition = false ;
97
97
}
98
+ // Transmits the current position
98
99
if (cmd[0 ] == ' T' )
99
100
{
100
101
Serial . print(" G:xyt:" );
@@ -103,15 +104,132 @@ void loop()
103
104
Serial . print(yPosition);
104
105
Serial . print(" \n " );
105
106
}
107
+ // Resets the mouse
106
108
if (cmd[0 ] == ' R' )
107
109
{
108
110
mouse. reset();
109
111
mouse2. reset();
110
112
}
113
+ // Manual moving
114
+ if (cmd[0 ] == ' r' )
115
+ {
116
+ moveRight = true ;
117
+ }
118
+ if (cmd[0 ] == ' r' && cmd[1 ] == ' s' )
119
+ {
120
+ moveRight = false ;
121
+ }
122
+ if (cmd[0 ] == ' l' )
123
+ {
124
+ moveLeft = true ;
125
+ }
126
+ if (cmd[0 ] == ' l' && cmd[1 ] == ' s' )
127
+ {
128
+ moveLeft = false ;
129
+ }
130
+
131
+
132
+
133
+ if (cmd[0 ] == ' u' )
134
+ {
135
+ moveUp = true ;
136
+ }
137
+ if (cmd[0 ] == ' u' && cmd[1 ] == ' s' )
138
+ {
139
+ moveUp = false ;
140
+ }
141
+ if (cmd[0 ] == ' d' )
142
+ {
143
+ moveDown = true ;
144
+ }
145
+ if (cmd[0 ] == ' d' && cmd[1 ] == ' s' )
146
+ {
147
+ moveDown = false ;
148
+ }
149
+ index = 0 ;
150
+ }
151
+
152
+ if (moveRight && spdLeft == 0 )
153
+ {
154
+ if (spdRight < 250 )
155
+ {
156
+ spdRight += 25 ;
157
+ motorAForward(spdRight);
158
+ delay (100 );
159
+ }
160
+ }
161
+ if (! moveRight)
162
+ {
163
+ if (spdRight > 0 )
164
+ {
165
+ spdRight -= 25 ;
166
+ motorAForward(spdRight);
167
+ delay (100 );
168
+ }
169
+ }
170
+ if (moveLeft && spdRight == 0 )
171
+ {
172
+ if (spdLeft < 250 )
173
+ {
174
+ spdLeft += 25 ;
175
+ motorABack(spdLeft);
176
+ delay (100 );
177
+ }
178
+ }
179
+ if (! moveLeft)
180
+ {
181
+ if (spdLeft > 0 )
182
+ {
183
+ spdLeft -= 25 ;
184
+ motorABack(spdLeft);
185
+ delay (100 );
186
+ }
111
187
}
112
- /** ************************ Serial communication end ************************/
113
- /** **************************************************************************/
114
188
189
+
190
+
191
+
192
+
193
+ if (moveUp && spdDown == 0 )
194
+ {
195
+ if (spdUp < 250 )
196
+ {
197
+ spdUp += 25 ;
198
+ motorBForward(spdUp);
199
+ delay (100 );
200
+ }
201
+ }
202
+ if (! moveUp)
203
+ {
204
+ if (spdUp > 0 )
205
+ {
206
+ spdUp -= 25 ;
207
+ motorBForward(spdUp);
208
+ delay (100 );
209
+ }
210
+ }
211
+ if (moveDown && spdUp == 0 )
212
+ {
213
+ if (spdDown < 250 )
214
+ {
215
+ spdDown += 25 ;
216
+ motorBBack(spdDown);
217
+ delay (100 );
218
+ }
219
+ }
220
+ if (! moveDown)
221
+ {
222
+ if (spdDown > 0 )
223
+ {
224
+ spdDown -= 25 ;
225
+ motorBBack(spdDown);
226
+ delay (100 );
227
+ }
228
+ }
229
+
230
+
231
+
232
+ /** ************************ Serial communication end ************************/
115
233
/** ***************************** Go-To/Tracking *****************************/
116
234
tmp1 = yPosition;
117
235
tmp2 = xPosition;
@@ -135,7 +253,7 @@ void loop()
135
253
{
136
254
spd1 = 250 ;
137
255
}
138
-
256
+
139
257
if ((abs (tmp2 - previousPosition2) <= 10 ) || (abs (Setpoint2 - tmp2) <= 10 ))
140
258
{
141
259
spd2 = 50 ;
@@ -199,8 +317,6 @@ void loop()
199
317
}
200
318
}
201
319
/** *************************** Go-To/Tracking end ***************************/
202
- /** **************************************************************************/
203
-
204
320
if (sendPosition)
205
321
{
206
322
Serial . print(" G:xy:" );
@@ -212,7 +328,6 @@ void loop()
212
328
sendPosition = false ;
213
329
}
214
330
}
215
-
216
331
/** *************************** Motor control commands **************************/
217
332
void motorAForward(int spd)
218
333
{
@@ -253,4 +368,13 @@ void motorBStop()
253
368
digitalWrite(motorBpinA, LOW );
254
369
digitalWrite(motorBpinB, LOW );
255
370
analogWrite(motorBpinB, 0 );
256
- }
371
+ }
372
+
373
+
374
+
375
+
376
+
377
+
378
+
379
+
380
+
0 commit comments