-
Notifications
You must be signed in to change notification settings - Fork 0
/
gimpyCode.ino
200 lines (175 loc) · 4.45 KB
/
gimpyCode.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
/*
* Authours: Taha Aziz and Calvin Tran (code basically all done by Taha)
* Date: June 12, 2019
* Description: Culminating robot code for Mr. French's TEJ4M course.
* The robot has two motorized arms (and two wheels) which it uses to drag itself along.
* We were going to only have four legs and no wheels but didn't have enough motors.
* It uses an ultrasonic sensor to detect walls.
*/
#include <Servo.h>
Servo s[10];
int curPos[10] = {0,0,0,0,0,0}; // variable to store the servo positions (not really used in the rest of the code)
const double position[2][10] = {{0, 10, 180, 180, 180, 10}, // Max and min positions for each servo
{114, 95, 90, 60, 90, 105}};// Numbers in first row and the down/back position, bottom row is the top/front position
// PING
int trigPin = 12; // Trigger
int echoPin = 13; // Echo
long duration, cm;
void setup() {
// attach each servo to a pin
s[0].attach(2);
s[1].attach(3);
s[2].attach(4);
s[3].attach(5);
s[4].attach(6);
s[5].attach(7);
// PING
Serial.begin (9600); // serial Port begin
pinMode(trigPin, OUTPUT); //define inputs and outputs
pinMode(echoPin, INPUT);
}
void loop() {
walk();
}
void stand(){
// reference standing position
swrite(70, 0);
swrite(0, 1);
swrite(130, 2);
swrite(70, 3);
swrite(0, 4);
swrite(130, 5);
}
void walk(){
int del = 500;
delay(del);
while(1){
// simutaneous leg action walk cycle
swrite(70, 0);
swrite(70, 3);
delay(del);
swrite(180, 1);
swrite(180, 4);
delay(del+350);
swrite(180, 2);
swrite(180, 5);
delay(del);
swrite(180, 0);
swrite(180, 3);
delay(del);
swrite(0, 1);
swrite(0, 4);
delay(100);
swrite(105, 2);
swrite(105, 5);
delay(del);
while (ping() < 100){
hop(); // Switched to a different walk cycle close to a wall
}
}
}
void hop(){
// Simutaneous leg action walk cycle with four motors (two don't move)
int del = 300;
swrite(180, 0);
swrite(180, 3);
delay(del);
swrite(180, 1);
swrite(180, 4);
swrite(180, 2);
swrite(180, 5);
delay(del);
swrite(0, 1);
swrite(0, 4);
swrite(40, 2);
swrite(40, 5);
unsigned long startTime = millis();
while (ping() < 40){
// turns to avoid a wall
unsigned long currentTime = millis();
if (startTime - currentTime > 15000){
// turns right if turning left doesn't distance it from the wall in a certain period of time (15 seconds)
turn(3, 0);
}
else{
turn(0, 3);
}
}
}
void turn(int x, int y){
// turn(0, 3) turns left and turn(3, 0) turns right
int del = 500;
stand();
swrite(70, x);
swrite(180, y);
delay(del);
swrite(180, 1);
swrite(180, 4);
delay(del);
swrite(180, 2);
swrite(180, 5);
delay(del);
swrite(180, x);
swrite(70, y);
delay(del);
swrite(0, 1);
swrite(0, 4);
delay(100);
swrite(90, 2);
swrite(90, 5);
delay(del);
swrite(70, x);
}
int ping(){
// The sensor is triggered by a HIGH pulse of 10 or more microseconds
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the signal, a HIGH pulse whose duration is the time from the sending of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// Convert the time into a distance
cm = (duration/2) / 29.1;
Serial.print(cm);
Serial.print("cm");
Serial.println();
return cm;
}
// Send an angle (p) from 0 to 180 and the motor will move to a position relative to how it was calibrated
void swrite(double p, int n){
// writes to the motor n and stores the position in curPos
s[n].write(angle(p, n));
curPos[n] = p;
}
double angle(double p, int n){
// takes in an angle from 0 to 180 and finds the respective angle for motor n based on the position's which were calibrated in the const array
double i = ((p/180) * abs((position[0][n] - position[1][n])));
if (position[0][n] > position[1][n]){
return (position[0][n] - i);
}else{
return (position[0][n] + i);
}
}
void test(){
// sweeps each pair of motors to test calibration
swrite(0, 0);
swrite(0, 3);
delay(3000);
swrite(180, 0);
swrite(180, 3);
delay(5000);
swrite(0, 1);
swrite(0, 4);
delay(3000);
swrite(180, 1);
swrite(180, 4);
delay(5000);
swrite(0, 2);
swrite(0, 5);
delay(3000);
swrite(180, 2);
swrite(180, 5);
delay(5000);
}