This repository was archived by the owner on Oct 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.cpp
More file actions
335 lines (262 loc) · 7.94 KB
/
Main.cpp
File metadata and controls
335 lines (262 loc) · 7.94 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include <stdio.h>
#include <stdint.h>
#include "PIO_LED.h"
#include "PIO_BUTTON.h"
#include <time.h>
#include "CSpider.h"
#include "CSpiderLeg.h"
#include <iostream>
#include <pthread.h>
#include <unistd.h>
void* first_move(void* Sp)//void* first_move(void*)
{
CSpider* Spider = (CSpider*) Sp;
Spider->SetSpeed(80);
std::cout<<"\nMove Forward\n";
Spider->MoveForward(20); //10 ft
Spider->Reset();
std::cout<<"\nRotate\n"; //MAY VARY ON DIFFERENT SPIDERS.
Spider->RotatelLeft(8); // Arbitrary value. It makes the spider rorate pretty close 180 degrees.
Spider->Reset();
std::cout<<"\nMove Forward Again\n";
Spider->MoveForward(20); //10 ft
Spider->Reset();
return NULL;
}
void* second_move(void* Sp)//void* first_move(void*)
{
bool alternate = true;
CSpider* Spider = (CSpider*) Sp;
Spider->SetSpeed(15);
Spider->BestDance(1);
/*
Spider->Stomp(8);
std::cout<<"\nReseting\n";
Spider->Reset();
//Spider->RotatelRight(2);
//Spider->RotatelLeft(2);
for(int i = 0; i < 3; i++){
Spider->BodyForward();
Spider->TiltForward();
Spider->TiltRight();
Spider->TiltBackward();
Spider->BodyBackward();
Spider->TiltLeft();
Spider->TiltNone();
}
Spider->ByeBye(3);
Spider->Reset();
Spider->SetSpeed(30);
for(int i=0; i<30; i++){
Spider->RotatelRight(1);
Spider->MoveParallelL(2);
//Spider->BodyUpDown(1);
}
std::cout<<"\nReseting\n";
Spider->Reset();
std::cout<<"\nBefore Wave\n";
Spider->ByeBye(3);
std::cout<<"\nReseting\n";
Spider->Reset();
std::cout<<"\nAfter Wave\n";
Spider->usleep(1000000);
Spider->BodyNone();
Spider->BodyBackward();
//Spider->usleep(1000000);
Spider->BodyNone();
Spider->DEMO_Rollover();
Spider->MoveForward(1);
std::cout<<"\nReseting\n";
Spider->Reset();
Spider->TiltForward();
Spider->TiltNone();
Spider->TiltBackward();
Spider->TiltNone();
Spider->BodyUpDown(1);
Spider->MoveBackward(1);
std::cout<<"\nReseting\n";
Spider->Reset();
Spider->TiltRight();
Spider->TiltNone();
Spider->TiltLeft();
Spider->TiltNone();
Spider->TiltRight();
Spider->TiltNone();
Spider->TiltLeft();
Spider->TiltNone();
Spider->ByeBye(3);*/
return NULL;
}
void* LEDsL(void*)
{
CPIO_LED LED_PIO;
uint32_t tickCount = OS_GetTickCount();
int led = 1;
int frequency = 8;
while (true)
{
#define handle_error_en(en, msg) \
do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
int s, t;
t = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
s = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
if (s != 0)
handle_error_en(s, "pthread_setcancelstate");
if (t != 0)
handle_error_en(s, "pthread_setcanceltype");
if(OS_GetTickCount()-tickCount > OS_TicksPerSecond()/frequency) {
tickCount = OS_GetTickCount();
led = led << 1;
if(led > 256) {
led = 1;
}
LED_PIO.SetLED(led);
}
}
return NULL;
}
void* LEDsR(void*)
{
CPIO_LED LED_PIO;
uint32_t tickCount = OS_GetTickCount();
int led = 1;
int frequency = 8;
while (true)
{
#define handle_error_en(en, msg) \
do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
int s, t;
t = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
s = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
if (s != 0)
handle_error_en(s, "pthread_setcancelstate");
if (t != 0)
handle_error_en(s, "pthread_setcanceltype");
if(OS_GetTickCount()-tickCount > OS_TicksPerSecond()/frequency) {
tickCount = OS_GetTickCount();
led = led >> 1;
if(led < 1) {
led = 256;
}
LED_PIO.SetLED(led);
}
}
return NULL;
}
void* LEDsB(void*)
{
CPIO_LED LED_PIO;
uint32_t tickCount = OS_GetTickCount();
int led;
int frequency = 16;
bool change = true;
while (true)
{
#define handle_error_en(en, msg) \
do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
int s, t;
t = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
s = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
if (s != 0)
handle_error_en(s, "pthread_setcancelstate");
if (t != 0)
handle_error_en(s, "pthread_setcanceltype");
if(OS_GetTickCount()-tickCount > OS_TicksPerSecond()/frequency) {
tickCount = OS_GetTickCount();
if(change){
led = 0;
change=false;
}
else{
led = 127;
change=true;
}
LED_PIO.SetLED(led);
}
}
return NULL;
}
void* exitProg(void* btn)
{
CPIO_BUTTON *BUTTON_PIO = (CPIO_BUTTON*) btn;
printf("Exit function\r\n");
sleep(1);
while((BUTTON_PIO->GetBUTTON() & 0x03) != 0)
{ }
exit(0);
return 0;
}
int main(int argc, char *argv[]){
CPIO_LED LED_PIO;
CPIO_BUTTON BUTTON_PIO;
CSpider Spider;
int buttons;
int prev_buttons;
unsigned int sleep(int GoToSleep);
printf("Before exit thread\r\n");
// Exit Program------------------------------------
pthread_t Ext;
pthread_create(&Ext, NULL, &exitProg, &BUTTON_PIO);
//-------------------------------------------------
printf("After exit thread\r\n");
printf("Spider Init & Standup\r\n");
if (!Spider.Init())
{
printf("Spilder Init failed\r\n");
}
else
{
if (!Spider.Standup())
printf("Spilder Standup failed\r\n");
}
Spider.SetSpeed(100);
pthread_t ledsL;
pthread_create(&ledsL, NULL, &LEDsL, NULL); //LEDs Left thread
printf("Main Funtionality\n");
while(true)
{
prev_buttons = buttons;
buttons = BUTTON_PIO.GetBUTTON();
if ((BUTTON_PIO.GetBUTTON() & 0x01)== 0) //KEY0 FIRST MOVE
{
if(buttons != prev_buttons)
{
pthread_cancel(ledsL);
printf("\n\nLEFT LEDs has been Stopped\n\n");
//sleep(10);
pthread_t t1, ledsR;
pthread_create(&t1, NULL, &first_move, &Spider); //Move Thread
pthread_create(&ledsR, NULL, &LEDsR, NULL); //LEDs Right thread
//-----Stop LEDs thread
void* result;
pthread_join(t1,&result);
printf("Joining\r\n");
pthread_cancel(ledsR);
printf("\n\nLEDs has been Stopped 2\n\n");
//---------------------
pthread_create(&ledsL, NULL, &LEDsL, NULL); //LEDs Left thread
}
}
if ((BUTTON_PIO.GetBUTTON() & 0x02)== 0) //KEY1
{
if(buttons != prev_buttons)
{
pthread_cancel(ledsL);
printf("\n\nLEFT LEDs has been Stopped\n\n");
//sleep(10);
pthread_t t2, ledsB;
pthread_create(&t2, NULL, &second_move, &Spider); //Move Thread
pthread_create(&ledsB, NULL, &LEDsB, NULL); //LEDs Right thread
//-----Stop LEDs thread
void* result;
pthread_join(t2,&result);
printf("Joining\r\n");
pthread_cancel(ledsB);
printf("\n\nLEDs has been Stopped 2\n\n");
pthread_create(&ledsL, NULL, &LEDsL, NULL); //LEDs Left thread
//---------------------
}
}
}
return 0;
}