-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThumperController.c
284 lines (230 loc) · 6.01 KB
/
ThumperController.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
Test WAV Volume.c
Version 0.94 for use with SimpleIDE 9.40 and its Simple Libraries
Play back a .wav file and try a few different volume settings.
http://learn.parallax.com/propeller-c-simple-devices/play-wav-files
*/
#include "simpletools.h"
#include "wavplayer.h"
#include "servo.h"
#include "ping.h"
int LEFT_SERVO_PIN = 3;
int RIGHT_SERVO_PIN = 4;
int HEAD_SERVO_PIN = 19;
int PINGER_PIN = 18;
int V_PANT_PIN = 14;
int H_PANT_PIN = 15;
//PWM Freq in KHz
HB25_FREQ = 9200;
int HB25_FULL_FORWARD_MS = 2200;
int HB25_NEUTRAL_MS = 1500;
int HB25_FULL_REVERSE_MS = 1000;
setupSDCard(){
int DO = 22, CLK = 23, DI = 24, CS = 25; // SD I/O pins
sd_mount(DO, CLK, DI, CS); // Mount SD card
}
playSound(const char soundFileName[]){
//const char techloop[] = {"techloop.wav"}; // Set up levels string
wav_play(soundFileName); // Pass to wav player
wav_volume(10); // Adjust volume
pause(165900);
//pause(1500000);
}
leftWheelsForward(){
pwm_set(LEFT_SERVO_PIN,0,HB25_FULL_FORWARD_MS);
pause(20);
}
stopLeftWheels(){
pwm_set(LEFT_SERVO_PIN,0,HB25_NEUTRAL_MS);
pause(20);
}
leftWheelsReverse(){
pwm_set(LEFT_SERVO_PIN,0,HB25_FULL_REVERSE_MS);
pause(20);
}
rightWheelsForward(){
pwm_set(RIGHT_SERVO_PIN,1,HB25_FULL_FORWARD_MS);
pause(20);
}
rightWheelsReverse(){
pwm_set(RIGHT_SERVO_PIN,1,HB25_FULL_REVERSE_MS);
pause(20);
}
stopRightWheels(){
pwm_set(RIGHT_SERVO_PIN,1,HB25_NEUTRAL_MS);
pause(20);
}
driveForward(){
print("driving forward\n");
leftWheelsForward();
rightWheelsForward();
pause(20);
}
driveReverse(){
print("driving reverse\n");
leftWheelsReverse();
rightWheelsReverse();
pause(20);
}
stop(){
print("stopping...\n");
stopLeftWheels();
stopRightWheels();
}
printPinInfo(int pin){
/*The pin's state. If the pin is an output, 1 = 3.3 V
and 0 = 0 V. If the pin is an input, 1 means V > 1.65 V, 0 means it's less.*/
int pinState = get_state(pin);
//I/O pin direction as seen by the cog that runs the function.
int pinDirection = get_direction(pin);
//In a register bit for I/O pin, either 1 or 0.
int pinOutput = get_output(pin);
print("pin %d state: %d\n", pin, pinState);
print("pin %d direction: %d\n", pin, pinDirection);
print("pin %d output: %d\n", pin, pinOutput);
}
sendLowSignal(int pin){
set_direction(pin, 1);
low(pin);
}
hb25Init(int pin){
print("hb-25 init started on pin: %d\n", pin);
sendLowSignal(pin);
pause(5000);
print("getting input from mc\n");
set_direction(pin, 0);
while( get_state(pin) == 0){
//print("Wating for hb-25 to start...\n");
}
sendLowSignal(pin);
printPinInfo(pin);
pause(20);
printPinInfo(pin);
}
hb25InitAll(){
pwm_start(HB25_FREQ);
hb25Init(LEFT_SERVO_PIN);
hb25Init(RIGHT_SERVO_PIN);
}
rampUp(){
//pwm_set(RIGHT_SERVO_PIN,0,HB25_FULL_REVERSE_MS);
print("ramping up...");
for(int i = HB25_NEUTRAL_MS; i <= HB25_FULL_FORWARD_MS; i++){
pwm_set(RIGHT_SERVO_PIN, 0, i);
pwm_set(LEFT_SERVO_PIN, 0, i);
pause(150);
print("%d\n", i);
}
}
rampUpReverse(){
print("ramping up reverse...");
for(int i = 1070; i >= HB25_FULL_REVERSE_MS; i--){
pwm_set(RIGHT_SERVO_PIN, 0, i);
pause(150);
print("%d\n", i);
}
}
rampDown(){
print("ramping down...");
for(int i = HB25_FULL_FORWARD_MS; i >= HB25_NEUTRAL_MS; i--){
pwm_set(RIGHT_SERVO_PIN, 0, i);
pwm_set(LEFT_SERVO_PIN, 0, i);
pause(150);
print("%d\n", i);
}
stop();
}
spinRight(){
rightWheelsForward();
leftWheelsReverse();
}
testServo(int pin){
//print("testing servo on pin %d\n", pin);
servo_angle(pin, 400); // P16 servo to 0 degrees
pause(1000); // ...for 3 seconds
// servo_angle(pin, 900);
servo_angle(pin, 1800); // P16 servo to 90 degrees
pause(1000); // ...for 3 seconds
//servo_angle(pin, 400); // P16 servo to 180 degrees
//pause(1000); // ...for 3 seconds
servo_stop();
}
testVPant(){
for(int i = 0; i < 4; i++){
testServo(V_PANT_PIN);
}
}
testHPant(){
for(int i = 0; i < 4; i++){
testServo(H_PANT_PIN);
}
}
testDrive(){
hb25InitAll();
driveForward();
pause(500);
stop();
pause(500);
driveReverse();
pause(500);
stop();
}
testPinger(int pin){
int cmDist = ping_cm(pin); // Get cm distance from Ping)))
print("cmDist = %d\n", cmDist); // Display distance
pause(200);
}
int main() // main function
{
int userInput;
setupSDCard();
while(1){
print("What would you like to do?");
scan("%d/n", &userInput);
if(userInput == 1){
leftWheelsForward();
//rightWheelsForward();
}else if(userInput == 2){
stop();
}else if (userInput == 3){
print("Playing sound\n");
playSound("techloop.wav");
}else if (userInput == 4){
wav_stop();
}else if (userInput == 5){
//hb25Init(RIGHT_SERVO_PIN);
hb25InitAll();
}else if (userInput ==6){
leftWheelsReverse();
//rightWheelsReverse();
}else if (userInput == 7){
pwm_stop();
return 0;
}else if (userInput == 8){
playSound("regper.wav");
}else if (userInput == 9){
rampUp();
rampDown();
}else if (userInput == 10){
rightWheelsForward();
}else if (userInput == 11){
rightWheelsReverse();
}else if (userInput == 12){
driveForward();
}else if (userInput == 13){
driveReverse();
}else if (userInput == 14){
spinRight();
}else if (userInput == 15){
testServo(HEAD_SERVO_PIN);
}else if(userInput == 16){
testPinger(PINGER_PIN);
}else if(userInput == 17){
cog_run(testVPant, 128);
cog_run(testHPant, 128);
}
//print("Playing sound\n");
//playSound("techloop.wav");
//playSound("regper.wav");
}
}