-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibAutonomous.c
91 lines (85 loc) · 2.42 KB
/
libAutonomous.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
/*
Awesome Messages.c
*/ // Include simple tools
//#include "Autonomous.h"
#include "Action.h"
#include "fdserial.h"
#include "PingerController.h"
#include "ServoController.h"
#include <stdbool.h>
fdserial *xbee;
int FORWARD_DISTANCE_THRESHOLD = 100;
int DIRECTION_CHANGE_DURATION_MS = 1000;
//8.28 volts draw
//.11 amps at idle draw and 5.79 amps with all 6 wheels running and 1.54 one side running .74 volts on the signal cable
//Find the new direction to move to
//return l for left r for right b for reverse
char findNewDirection(){
rotateCenter(HEAD_SERVO_PIN);
int centerDistance = getPingCMDistance(PINGER_PIN);
rotateLeft(HEAD_SERVO_PIN);
int leftDistance = getPingCMDistance(PINGER_PIN);
rotateRight(HEAD_SERVO_PIN);
int rightDistance = getPingCMDistance(PINGER_PIN);
rotateCenter(HEAD_SERVO_PIN);
if(leftDistance > rightDistance){
return 'l';
}else{
return 'r';
}
}
void driveAutonomously(){
//reset
stop();
rotateCenter(HEAD_SERVO_PIN);
while(1){
int forwardDistance = getPingCMDistance(PINGER_PIN);
while(forwardDistance < FORWARD_DISTANCE_THRESHOLD){
stop();
driveReverse();
pause(DIRECTION_CHANGE_DURATION_MS);
stop();
char newDirection = findNewDirection();
if(newDirection == 'l'){
spinLeft();
pause(DIRECTION_CHANGE_DURATION_MS);
stop();
}else if(newDirection == 'r'){
spinRight();
pause(DIRECTION_CHANGE_DURATION_MS);
stop();
}
forwardDistance = getPingCMDistance(PINGER_PIN);
}
driveForward();
}
}
int main(){
//Setup fdserial
hb25InitAll();
xbee = fdserial_open(9, 8, 0, 9600);
writeChar(xbee, CLS);
dprint(xbee, "Click this terminal, \n");
dprint(xbee, "and type on keybaord...\n\n");
int frontDistance = 0;
char userInput;
while(1){
userInput = -1;
userInput = fdserial_rxChar(xbee);
if(userInput != -1){
dprint(xbee, "You typed: %c\n", userInput);
if(userInput == '1'){
driveForward();
}else if(userInput == '2'){
testServo(17);
}else if(userInput == '3'){
testPing(12);
}else if(userInput == '4'){
driveAutonomously();
}else if(userInput == '9'){
//Stop Driving
stop();
}
}
}
}