Skip to content

Commit a27940d

Browse files
author
mandimastro
committed
Addition of manual movement
Command set extended
1 parent ca39a36 commit a27940d

File tree

1 file changed

+175
-51
lines changed

1 file changed

+175
-51
lines changed

Hipparchus_firmware/Hipparchus_firmware.pde

+175-51
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,101 @@
11
#include <ps2.h>
22

3-
//Serial communication vars
43
char cmd[20];
5-
int index;
4+
int index = 0;
65

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;
912

10-
// Motor's pins
1113
int motorApinA = 3;
1214
int motorApinB = 5;
1315
int motorBpinA = 9;
1416
int motorBpinB = 11;
1517

18+
int spdLeft = 0;
19+
int spdRight = 0;
20+
int spdUp = 0;
21+
int spdDown = 0;
22+
1623
//Mouse initialisation. PS2Mouse(clock, data)
1724
PS2Mouse mouse(12, 7);
1825
PS2Mouse mouse2(8, 2);
1926

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;
2334

2435
MouseInfo mouseInfo;
2536
MouseInfo mouseInfo2;
2637

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;
3546

3647
void setup()
3748
{
49+
Serial.begin(115200);
50+
3851
pinMode(motorApinA, OUTPUT);
3952
pinMode(motorApinB, OUTPUT);
4053
pinMode(motorBpinA, OUTPUT);
4154
pinMode(motorBpinB, OUTPUT);
4255

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
5257

5358
mouse.init();
5459
mouse2.init();
5560

56-
goTo1 = false;
57-
goTo2 = false;
58-
sendPosition = false;
5961
}
60-
6162
void loop()
6263
{
64+
while (Serial.available())
65+
{
66+
cmd[index] = Serial.read();
67+
delay(1);
68+
index++;
69+
}
70+
cmd[index] = '\0';
71+
72+
6373
/************************ Mouse routines **************************/
6474
mouse.getData(&mouseInfo);
6575
mouse2.getData(&mouseInfo2);
6676
xPosition = mouseInfo.cX;
67-
yPosition = mouseInfo2.cY;
77+
yPosition = mouseInfo2.cX;
6878
/******************************************************************/
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-
8179
if (index > 0)
82-
{
80+
{
81+
//Goes to specific set points: G100,200
8382
if (cmd[0] == 'G'){
8483
Setpoint2 = atoi(strtok((cmd+1), ","));
8584
Setpoint1 = atoi(strtok(NULL, ","));
86-
85+
8786
previousPosition1 = yPosition;
8887
previousPosition2 = xPosition;
8988
goTo1 = true;
9089
goTo2 = true;
9190
}
91+
//Stops the motors
9292
if (cmd[0] == 'S')
9393
{
9494
goTo1 = false;
9595
goTo2 = false;
9696
sendPosition = false;
9797
}
98+
//Transmits the current position
9899
if (cmd[0] == 'T')
99100
{
100101
Serial.print("G:xyt:");
@@ -103,15 +104,132 @@ void loop()
103104
Serial.print(yPosition);
104105
Serial.print("\n");
105106
}
107+
//Resets the mouse
106108
if (cmd[0] == 'R')
107109
{
108110
mouse.reset();
109111
mouse2.reset();
110112
}
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+
}
111187
}
112-
/************************** Serial communication end ************************/
113-
/****************************************************************************/
114188

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 ************************/
115233
/******************************* Go-To/Tracking *****************************/
116234
tmp1 = yPosition;
117235
tmp2 = xPosition;
@@ -135,7 +253,7 @@ void loop()
135253
{
136254
spd1 = 250;
137255
}
138-
256+
139257
if ((abs(tmp2 - previousPosition2) <= 10) || (abs(Setpoint2 - tmp2) <= 10))
140258
{
141259
spd2 = 50;
@@ -199,8 +317,6 @@ void loop()
199317
}
200318
}
201319
/***************************** Go-To/Tracking end ***************************/
202-
/****************************************************************************/
203-
204320
if (sendPosition)
205321
{
206322
Serial.print("G:xy:");
@@ -212,7 +328,6 @@ void loop()
212328
sendPosition = false;
213329
}
214330
}
215-
216331
/***************************** Motor control commands **************************/
217332
void motorAForward(int spd)
218333
{
@@ -253,4 +368,13 @@ void motorBStop()
253368
digitalWrite(motorBpinA, LOW);
254369
digitalWrite(motorBpinB, LOW);
255370
analogWrite(motorBpinB, 0);
256-
}
371+
}
372+
373+
374+
375+
376+
377+
378+
379+
380+

0 commit comments

Comments
 (0)