-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.cpp
42 lines (37 loc) · 897 Bytes
/
demo.cpp
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
#include <cstdio>
#include <cmath>
#include "GPIOlib.h"
using namespace std;
using namespace GPIO;
int readingLeft=0,readingRight=0;
void turn(int angle){
turnTo(angle);
}
int main()
{
controlLeft(FORWARD,10);
controlRight(FORWARD,10);
while(1)
{
resetCounter();
delay(100);
getCounter(&readingLeft,&readingRight);
if(readingLeft==-1||readingRight==-1)
{
printf("Error!\n");
continue;
}
//Distance is in mm.
double distanceLeft=readingLeft*63.4*M_PI/390;
double distanceRight=readingRight*63.4*M_PI/390;
printf("Left wheel moved %.2lf cm, right wheel moved %.2lf cm in last second.\n",distanceLeft/10,distanceRight/10);
if(distanceLeft>distanceRight){
controlLeft(FORWARD,8);
controlRight(FORWARD,10);
}
else if(distanceLeft<distanceRight){
controlRight(FORWARD,8);
controlLeft(FORWARD,10);
}
}
}