-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovement.c
156 lines (144 loc) · 3.6 KB
/
movement.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
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
/*
* movement.c
*
* Created on: Feb 3, 2023
* Author: mdwells
*/
#include "open_interface.h"
#include "movement.h"
#include "lcd.h"
#include "timer.h"
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <inc/tm4c123gh6pm.h>
#include <uart-interrupt.h>
//Moves the bot some distance
double move_forward(oi_t *sensor_data, double distance_mm)
{
char string[3];
double sum = 0;
oi_setWheels(100, 100); //move forward slowly
while (sum < distance_mm)
{
oi_update(sensor_data);
sum += sensor_data->distance; //accumulate distance
if (sensor_data->bumpRight)
{
stop();
sprintf(string, "B1");
uart_sendStr(string);
return sum;
}
if (sensor_data->bumpLeft)
{
stop();
sprintf(string, "B0");
uart_sendStr(string);
return sum;
}
if (sensor_data->cliffLeft)
{
stop();
sprintf(string, "C0");
uart_sendStr(string);
return sum;
}
if (sensor_data->cliffFrontLeft)
{
stop();
sprintf(string, "C1");
uart_sendStr(string);
return sum;
}
if (sensor_data->cliffFrontRight)
{
stop();
sprintf(string, "C2");
uart_sendStr(string);
return sum;
}
if (sensor_data->cliffRight)
{
stop();
sprintf(string, "C3");
uart_sendStr(string);
return sum;
}
if (sensor_data->cliffLeftSignal > 2700)
{
stop();
sprintf(string, "L0");
uart_sendStr(string);
return sum;
}
if (sensor_data->cliffFrontLeftSignal > 2700)
{
stop();
sprintf(string, "L1");
uart_sendStr(string);
return sum;
}
if (sensor_data->cliffFrontRightSignal > 2700)
{
stop();
sprintf(string, "L2");
uart_sendStr(string);
return sum;
}
if (sensor_data->cliffRightSignal > 2700)
{
stop();
sprintf(string, "L3");
uart_sendStr(string);
return sum;
}
}
oi_setWheels(0, 0); //stop
return sum;
}
//Moves the bot some distance
double move_backward(oi_t *sensor_data, double distance_mm)
{
double sum = 0;
oi_setWheels(-100, -100); //move forward slowly
while (sum < distance_mm)
{
oi_update(sensor_data);
sum -= sensor_data->distance; //accumulate distance
}
oi_setWheels(0, 0); //stop
return (-1*sum);
}
//stops the bot and backs up to scan
void stop()
{
oi_setWheels(0, 0); //stop
timer_waitMillis(500); //Wait half a second to prevent movement into hole.
}
double turn_right(oi_t *sensor, double degrees)
{
oi_setWheels(-100, 100);
double sum = 0;
while (sum < degrees)
{
oi_update(sensor);
sum -= sensor->angle; // use -> notation since pointer
}
oi_setWheels(0, 0); //stop
return sum;
}
double turn_left(oi_t *sensor, double degrees)
{
oi_setWheels(100, -100);
double sum = 0;
while (sum < degrees)
{
oi_update(sensor);
sum += sensor->angle; // use -> notation since pointer
}
oi_setWheels(0, 0); //stop
return -1 * sum;
}