-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab_6_Test.c
130 lines (119 loc) · 2.52 KB
/
Lab_6_Test.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#pragma config(StandardModel, "EV3_REMBOT")
int distanceToObstacle = 0;
bool alreadyTurning = false;
float degreeReading;
bool noObstacle = true;
float tolerance = radiansToDegrees(0.1);
float get_time(float d)
{
d = d * 1200;
d = d / 11;
return d;
}
task RangeSensorTask()
{
sensorReset(S4);
while(true)
{
distanceToObstacle = getUSDistance(S4);
noObstacle = (distanceToObstacle > 10);
}
}
task gyroDegreeReader()
{
resetGyro(S2);
while(true)
degreeReading = getGyroDegrees(S2);
}
void stopMotors()
{
setMotorSpeed(motorB, 0);
setMotorSpeed(motorC, 0);
}
void point_Turn_Left(int desiredSpeed, float desiredDegree)
{
repeatUntil(abs(degreeReading - tolerance) > desiredDegree)
{
setMotorSync(motorB,motorC, -100,desiredSpeed);
}
stopMotors();
}
void point_Turn_Right(int desiredSpeed, float desiredDegree)
{
repeatUntil(abs(degreeReading + tolerance) > desiredDegree)
{
setMotorSync(motorB,motorC, 100, desiredSpeed);
}
stopMotors();
}
void point_Turn(int desiredSpeed, float desiredDegree)
{
resetGyro(S2);
if( desiredDegree < 0 )
point_Turn_Right(desiredSpeed, abs(desiredDegree));
else
point_Turn_Left(desiredSpeed, abs(desiredDegree));
}
void move_Forward(int desiredSpeed, int distance)
{
float t = get_time(distance);
setMotorSync(motorB,motorC,0,25);
sleep(t);
}
void TurnAroundObstacle()
{
alreadyTurning = true;
displayTextLine(2,"First Turn");
point_Turn(25, 90);
displayTextLine(2,"Moving Forward");
move_Forward(50, 20);
displayTextLine(2,"Second Turn");
point_Turn(25, -90);
displayTextLine(2,"Moving Forward");
move_Forward(50, 25);
displayTextLine(2,"Third Turn");
point_Turn(25, -90);
displayTextLine(2,"Moving Forward");
move_Forward(50, 20);
displayTextLine(2,"Forth Turn");
point_Turn(25, 90);
displayTextLine(2,"Moving Forward");
alreadyTurning = false;
}
task displayTask()
{
while(true)
{
displayTextLine(4,"Range to Obstacle: %d", distanceToObstacle);
displayTextLine(5,"degreeReading: %d", degreeReading);
}
}
task motionController()
{
while(true)
{
if(noObstacle && (!alreadyTurning))
{
setMotorSync(motorB,motorC,0,25);
}
else if (!alreadyTurning)
{
stopMotors();
TurnAroundObstacle();
//waitUntil(!alreadyTurning);
}
else
displayTextLine(3,"Waiting");
}
}
task main()
{
startTask(motionController);
startTask(RangeSensorTask);
startTask(gyroDegreeReader);
startTask(displayTask);
while(true)
{
sleep(2000);
}
}