-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.c
107 lines (91 loc) · 2.56 KB
/
app.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
#include "app.h" // <= Su propia cabecera
#include "sapi.h" // <= Biblioteca sAPI
#define SERVO_N SERVO0
bool_t goesForward = false; //Variable global de control para saber si el auto esta yendo para adelante
int main( void )
{
boardConfig();
uartConfig(UART_USB,115200);
//CONFIG SENSOR
ultrasonicSensorConfig(ULTRASONIC_SENSOR_0, ULTRASONIC_SENSOR_ENABLE);
delay(100);
//CONFIG SERVO
bool_t value = 0;
uint8_t servoAngle = 115;
value = servoConfig(0, SERVO_ENABLE);
value = servoConfig(SERVO_N, SERVO_ENABLE_OUTPUT);
value = servoWrite(SERVO_N, servoAngle);
servoAngle = servoRead(SERVO_N);
delay(100);
//CONFIG MOTORES
gpioConfig(T_COL0,GPIO_OUTPUT); //Motor1 Front Left
gpioConfig(GPIO1,GPIO_OUTPUT);
gpioConfig(GPIO8,GPIO_OUTPUT); //Motor2 Front Right
gpioConfig(GPIO3,GPIO_OUTPUT);
//GPIO0 y GPIO2 estan en uso por el sensor, por lo cual usamos en su lugar T_COL0 y GPIO8 respectivamente
// Hacemos varias lecturas iniciales con el sensor para tener un valor exacto de distancia inicial
uint32_t distance;
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
while(TRUE){
printf("Distance: %d cm.\r\n", distance);
gpioToggle(LEDR); //Para chequear que funciona
if(distance <= 15){
moveStop();
delay(100);
moveBackward();
turnRight();
}else{
moveForward();
}
distance = readPing();
};
return 0;
}
int readPing(){
delay(70);
int cm = ultrasonicSensorGetDistance(ULTRASONIC_SENSOR_0, CM);
if(cm == 0){
cm = 250;
}
return cm;
}
void moveStop(){
gpioWrite(T_COL0, false);
gpioWrite(GPIO1, false);
gpioWrite(GPIO8, false);
gpioWrite(GPIO3, false);
goesForward = false;
}
void moveForward(){
if(!goesForward){
goesForward=true;
gpioWrite(T_COL0, true);
gpioWrite(GPIO1, false);
gpioWrite(GPIO8, true);
gpioWrite(GPIO3, false);
}
}
void moveBackward(){
goesForward=false;
gpioWrite(T_COL0, false);
gpioWrite(GPIO1, true);
gpioWrite(GPIO8, false);
gpioWrite(GPIO3, true);
}
void turnRight(){
goesForward = false;
gpioWrite(T_COL0, true);
gpioWrite(GPIO1, false);
gpioWrite(GPIO8, false);
gpioWrite(GPIO3, true);
delay(500);
moveStop();
moveForward();
}