-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathmichelino.ino
239 lines (210 loc) · 7.32 KB
/
michelino.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Michelino
// Robot Vehicle firmware for the Arduino platform
//
// Copyright (c) 2013 by Miguel Grinberg
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is furnished
// to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
// AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
/**
* @file michelino.ino
* @brief Arduino robot vehicle firmware.
* @author Miguel Grinberg
*/
#define LOGGING
// Device drivers
// Enable one driver in each category
// Motor controller:
#define ENABLE_ADAFRUIT_MOTOR_DRIVER
//#define ENABLE_ARDUINO_MOTOR_DRIVER
// Distance sensor
#define ENABLE_NEWPING_DISTANCE_SENSOR_DRIVER
// Remote control:
//#define ENABLE_BLUESTICK_REMOTE_CONTROL_DRIVER
#define ENABLE_ROCKETBOT_REMOTE_CONTROL_DRIVER
// constants
#define TOO_CLOSE 10 /**< distance to obstacle in centimeters */
#define MAX_DISTANCE (TOO_CLOSE * 20) /**< maximum distance to track with sensor */
#define RANDOM_ANALOG_PIN 5 /**< unused analog pin to use as random seed */
#define BT_RX_PIN 16 /**< RX pin for Bluetooth communcation */
#define BT_TX_PIN 17 /**< TX pin for Bluetooth communcation */
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(BT_RX_PIN, BT_TX_PIN);
#ifdef ENABLE_ADAFRUIT_MOTOR_DRIVER
#include <AFMotor.h>
#include "adafruit_motor_driver.h"
#define LEFT_MOTOR_INIT 1
#define RIGHT_MOTOR_INIT 3
#endif
#ifdef ENABLE_ARDUINO_MOTOR_DRIVER
#include "arduino_motor_driver.h"
#define LEFT_MOTOR_INIT 12, 3, 9
#define RIGHT_MOTOR_INIT 13, 11, 8
#endif
#ifdef ENABLE_NEWPING_DISTANCE_SENSOR_DRIVER
#include <NewPing.h>
#include "newping_distance_sensor.h"
#define DISTANCE_SENSOR_INIT 14,15,MAX_DISTANCE
#endif
#ifdef ENABLE_BLUESTICK_REMOTE_CONTROL_DRIVER
#include "bluestick_remote_control.h"
#define REMOTE_CONTROL_INIT
#endif
#ifdef ENABLE_ROCKETBOT_REMOTE_CONTROL_DRIVER
#include "rocketbot_remote_control.h"
#define REMOTE_CONTROL_INIT 10,50
#endif
#include "logging.h"
#include "moving_average.h"
namespace Michelino
{
class Robot
{
public:
/*
* @brief Class constructor.
*/
Robot()
: leftMotor(LEFT_MOTOR_INIT), rightMotor(RIGHT_MOTOR_INIT),
distanceSensor(DISTANCE_SENSOR_INIT),
distanceAverage(TOO_CLOSE * 10),
remoteControl(REMOTE_CONTROL_INIT)
{
initialize();
}
/*
* @brief Initialize the robot state.
*/
void initialize()
{
randomSeed(analogRead(RANDOM_ANALOG_PIN));
remote();
}
/*
* @brief Update the state of the robot based on input from sensor and remote control.
* Must be called repeatedly while the robot is in operation.
*/
void run()
{
unsigned long currentTime = millis();
int distance = distanceAverage.add(distanceSensor.getDistance());
RemoteControlDriver::command_t remoteCmd;
bool haveRemoteCmd = remoteControl.getRemoteCommand(remoteCmd);
log("state: %d, currentTime: %lu, distance: %u remote: (%d,l:%d,r:%d,k:%d)\n",
state, currentTime, distance,
haveRemoteCmd, remoteCmd.left, remoteCmd.right, remoteCmd.key);
if (remoteControlled()) {
if (haveRemoteCmd) {
switch (remoteCmd.key) {
case RemoteControlDriver::command_t::keyF1:
// start "roomba" mode
move();
break;
case RemoteControlDriver::command_t::keyNone:
// this is a directional command
leftMotor.setSpeed(remoteCmd.left);
rightMotor.setSpeed(remoteCmd.right);
break;
default:
break;
}
}
}
else {
// "roomba" mode
if (haveRemoteCmd && remoteCmd.key == RemoteControlDriver::command_t::keyF1) {
// switch back to remote mode
remote();
}
else {
if (moving()) {
if (obstacleAhead(distance))
turn(currentTime);
}
else if (turning()) {
if (doneTurning(currentTime, distance))
move();
}
}
}
}
protected:
void remote()
{
leftMotor.setSpeed(0);
rightMotor.setSpeed(0);
state = stateRemote;
}
void move()
{
leftMotor.setSpeed(255);
rightMotor.setSpeed(255);
state = stateMoving;
}
void stop()
{
leftMotor.setSpeed(0);
rightMotor.setSpeed(0);
state = stateStopped;
}
bool obstacleAhead(unsigned int distance)
{
return (distance <= TOO_CLOSE);
}
bool turn(unsigned long currentTime)
{
if (random(2) == 0) {
leftMotor.setSpeed(-255);
rightMotor.setSpeed(255);
}
else {
leftMotor.setSpeed(255);
rightMotor.setSpeed(-255);
}
state = stateTurning;
endStateTime = currentTime + random(500, 1000);
}
bool doneTurning(unsigned long currentTime, unsigned int distance)
{
if (currentTime >= endStateTime)
return (distance > TOO_CLOSE);
return false;
}
bool moving() { return (state == stateMoving); }
bool turning() { return (state == stateTurning); }
bool stopped() { return (state == stateStopped); }
bool remoteControlled() { return (state == stateRemote); }
private:
Motor leftMotor;
Motor rightMotor;
DistanceSensor distanceSensor;
MovingAverage<unsigned int, 3> distanceAverage;
RemoteControl remoteControl;
enum state_t { stateStopped, stateMoving, stateTurning, stateRemote };
state_t state;
unsigned long endStateTime;
};
};
Michelino::Robot robot;
void setup()
{
Serial.begin(9600);
BTSerial.begin(9600);
}
void loop()
{
robot.run();
}