forked from Antanasici/Two-wheeled-robot-Bingo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWallFollowing_Bingo_V0.6.ino
229 lines (165 loc) · 5.47 KB
/
WallFollowing_Bingo_V0.6.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
/*
HC-SR04 Ping distance sensor
VCC to Arduino 5V
GND to Arduino GND
Echo to Arduino pin 13
Trig to Arduino pin 12
More info at: http://goo.gl/kJ8Gl
Original code improvements to the Ping sketch sourced from Trollmaker.com
Some code and wiring inspired by http://en.wikiversity.org/wiki/User:Dstaub/robotcar
Modified by Tolson Winters (Aug 27, 2014) for simplified serial monitor reading.
Modified by Antanas & Rimma (April 4, 2017) for "Bingo" project.
*/
/***************************************************************
// MOTOR CONTROL WITH MD-25 DRIVER BOARD
//**************************************************************/
//http://www.seattlerobotics.org/encoder/200108/using_a_pid.html
#include <Wire.h>
#include <MD25IIC.h>
//ultrasonic sensors
//front sensors
#define trigPin A0
#define echoPin A1
//----------------------------------------
// Data
//----------------------------------------
#define MODE 0x34
//----------------------------------------
//
// MD25IIC METHODS:
// ----------------------- ------------------------
// | MOTOR | | BOARD |
// -------------------------- |-----------------------|
// 1 | setMotor1Speed(byte) | | getAddress() |
// | getMotor1Encoder() | | setMode(byte) |
// | getLmotorCurrent() | | getBattery() |
// -------------------------| | enableTimeOut(T/F) |
// 2 | setMotor2Speed(byte) | | enableController(T/F) |
// | getRmotorEncoder() | | resetEncoders() |
// | getRmotorCurrent() | | setAcceleration(byte) |
// -------------------------| |-----------------------|
MD25IIC MyBoard;
/***************************************************************
// Setup function
//**************************************************************/
/***************************************************************
// Return angle (between 0 and 359º)
//**************************************************************/
/***************************************************************
// Setup function
//**************************************************************/
#define trigPin A0
#define echoPin A1
void setup() {
Serial.begin (115200);
MyBoard.enableTimeOut(true); // Stop motor if no command sent // within a 2s window
MyBoard.enableController(true); // Enable On-board speed controller
MyBoard.setMode(0);
MyBoard.resetEncoders(); // Reset (both) encoders to 0000
MyBoard.setAcceleration(10); // Increase acceleration (default = 5)
MyBoard.setMotor1Speed(128); // The left motor is initially stoped
MyBoard.setMotor2Speed(128); // The right motor is initially stoped
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
follow_wall();
}
void follow_wall() {
//motor1 left speed 140 goin forward
//motor2 rigg
MyBoard.setMotor1Speed(135); // 152 150
MyBoard.setMotor2Speed(135); // 148 150
Serial.print(sensor());
Serial.println(" cm");
do {
Serial.print(sensor());
Serial.println(" cm");
// MyBoard.setMotor1Speed(150); // 152
// MyBoard.setMotor2Speed(150); // 148
///
if (sensor() < 25 )
{
Serial.println(sensor());
Serial.println(" Turning left");
// MyBoard.setMotor1Speed(130); // 150 //152 150
MyBoard.setMotor2Speed(135); // 151 160
// print_encoders();
}
else if (sensor() > 30)// ( diff < 0 )
{
Serial.println(sensor());
Serial.println(" Turning right");
MyBoard.setMotor1Speed(135); //151 // 152
/// MyBoard.setMotor2Speed(130); //149
// print_encoders();
}
else if (sensor() > 25 && sensor() < 30)// ( diff < 0 )
{
Serial.println(sensor());
Serial.println(" go straight");
MyBoard.setMotor1Speed(135); //151 // 152
MyBoard.setMotor2Speed(135); //149
// print_encoders();
}
else
{
// Serial.print(diff);
Serial.println(sensor());
Serial.println(" STOP");
// Serial.println(" Going straight");
MyBoard.setMotor1Speed(128); // 152
MyBoard.setMotor2Speed(128); // 148
// print_encoders();
}
}
while (MyBoard.getMotor2Encoder() < 100000);
stop_motors();
MyBoard.resetEncoders();
}
int sensor() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
return distance;
}
void follow_wall_using_sensors( int obs_distance ) {
MyBoard.setMotor1Speed(150); // 152
MyBoard.setMotor2Speed(150); // 148
if (sensor() < obs_distance)
{
Serial.println(sensor());
Serial.println(" Turning Right");
MyBoard.setMotor1Speed(150); // 150
MyBoard.setMotor2Speed(152); // 151
}
else if (sensor() > obs_distance)// ( diff < 0 )
{
Serial.println(sensor());
Serial.println(" Turning Left");
MyBoard.setMotor1Speed(152); //151
MyBoard.setMotor2Speed(150); //149
}
else
{
// Serial.print(diff);
Serial.println(sensor());
Serial.println(" GOING STRAIGHT");
// Serial.println(" Going straight");
MyBoard.setMotor1Speed(150); // 152
MyBoard.setMotor2Speed(150); // 148
}
//while( 1==1);
// while (MyBoard.getMotor1Encoder() <= encoder_counts);
// stop_motors();
// print_encoders();
// MyBoard.resetEncoders();
}
void stop_motors() {
MyBoard.setMotor1Speed(128);
MyBoard.setMotor2Speed(128);
}