-
Notifications
You must be signed in to change notification settings - Fork 0
/
task3.c
114 lines (93 loc) · 2.08 KB
/
task3.c
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "abdrive.h"
#include "simpletext.h"
#include "simpletools.h"
#include "ping.h
int compensation = 0;
static void turnThroughAngle(int angle)
{
int ticks = (int) round(angle*0.284);
if (angle > 0)
{
if (compensation == 0)
{
drive_goto(-ticks , ticks - 1);
compensation = 1;
}
else
{
drive_goto(-ticks , ticks);
compensation = 0;
}
}
else
{
if (compensation == 0)
{
drive_goto(-ticks + 1, ticks );
compensation = 1;
}
else
{
drive_goto(-ticks , ticks);
compensation = 0;
}
}
}
static int LEFT = 0, RIGHT = 1;
int getDistanceFromTheWall(int wall) {
low(26);
low(27);
int irLeft = 0;
int irRight = 0;
for(int dacVal = 0; dacVal < 160; dacVal += 8) {
dac_ctr(26, 0, dacVal);
freqout(11, 1, 38000);
irLeft += input(10);
dac_ctr(27, 1, dacVal);
freqout(1, 1, 38000);
irRight += input(2);
}
if (wall == LEFT) {
return irLeft;
} else if (wall == RIGHT) {
return irRight;
}
//in case of the wrong input
printf("ERROR in 'getDistanceFromTheWall' function \n");
return -1;
}
int main(int argc, const char* argv[])
{
// Drive ahead nice and slow
int x = 18;
int temp = 0;
float pi = 3.14159265;
while(ping_cm(8) > 10)
{
drive_goto(x, x);
// Read the left and right sensors
int irLeft = getDistanceFromTheWall(LEFT);
if (temp == 0)
{
temp = irLeft;
}
printf("%d\n", temp);
printf("%d\n", irLeft);
if (temp != irLeft)
{
printf("THis is x1 : %d", temp);
printf("THis is x2 : %d", irLeft);
float angleinradian = atan((temp*10 - irLeft*10)/(x*3.25));
int angle = (int) (angleinradian * 180.0 /pi);
printf("ANGLE TO TURN %d\n", angle);
turnThroughAngle(angle * -1);
}
temp = irLeft;
pause(5);
}
return 0;
}