-
Notifications
You must be signed in to change notification settings - Fork 1
/
Vehicle.ino
133 lines (116 loc) · 3.1 KB
/
Vehicle.ino
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
// constants
const int runTime = 30;
const int close = 10;
#include <SoftwareSerial.h>
/* Set the pins for the bluetooth slave */
SoftwareSerial bluetooth(16, 17);
/* Downloaded from http://playground.arduino.cc/Code/NewPing */
#include <NewPing.h>
/* Downloaded from https://github.com/adafruit/Adafruit-Motor-Shield-library */
#include <AFMotor.h>
#include "Motor.h"
#include "BluetoothControl.h"
class Car{
public:
/* Setting the pins for the left, right, distance sensor( Trig and Echo) and the maximum distance */
Car()
: left( 1 ), right( 3 ), sensor( 14, 15, 200 ){
setCar();
}
void setCar(){
remote();
}
/* The car starts in the remote control phase and if the grab button is release, goes to auto drive */
void start(){
long time = millis();
int distance = 0;
// Not all signals are returned and thus a 0 value is given which confuses the vehicle, so if the sensor gets a 0, assume no signal was bounced back
if (sensor.ping_cm() <= 0)
distance = 200;
else
distance = sensor.ping_cm();
Signal BTSignal;
bool haveBTSignal = blueStick.getSignal(BTSignal); // Get signal from bluetooth
/* If mode is in control mode, set motor speed base on signal inputs */
if ( mode == control ) {
if ( haveBTSignal ) {
if ( BTSignal.selfDrive == true )
move();
else {
left.setSpeed(BTSignal.left);
right.setSpeed(BTSignal.right);
}
}
}
/* Self driving vehicle */
else {
/* Set back to control mode */
if (haveBTSignal && BTSignal.selfDrive == true){
remote();
BTSignal.selfDrive = false;
}
/* statments for the vehicle to move on its own */
else {
if ( mode == stopped )
return;
else if ( mode == moving )
if ( distance <= close )
turn(time);
else if ( mode == turning )
if (finishTurning(time, distance))
move();
}
}
}
protected:
void remote(){
left.setSpeed(0);
right.setSpeed(0);
mode = control;
}
/* Move forward on by itself */
void move(){
left.setSpeed(255);
right.setSpeed(255);
mode = moving;
}
void stop(){
left.setSpeed(0);
right.setSpeed(0);
mode = stopped;
}
bool turn(unsigned long time){
/* Base on Random values, turn left or right when sensor picks up something too close */
if (random(2) == 0) {
left.setSpeed(-255);
right.setSpeed(255);
}
else {
left.setSpeed(255);
right.setSpeed(-255);
}
mode = turning;
endmodeTime = time + random(500, 1000);
}
bool finishTurning(unsigned long time, unsigned int distance){
if (time >= endmodeTime)
return (distance > close);
return false;
}
private:
Motor left;
Motor right;
NewPing sensor;
enum carMode { stopped, moving, turning, control };
carMode mode;
unsigned long endmodeTime;
BluetoothControl blueStick;
};
Car myCar;
void setup(){
Serial.begin(9600);
bluetooth.begin(9600);
}
void loop(){
myCar.start();
}