-
Notifications
You must be signed in to change notification settings - Fork 0
/
Servo_PS2.ino
509 lines (458 loc) · 12.7 KB
/
Servo_PS2.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#include <Servo.h>
#include <Wire.h>
#include "PS2X_lib.h" //for v1.6
#include "Adafruit_PWMServoDriver.h"
// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
// you can also call it with a different address you want
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);
// Depending on your servo make, the pulse width min and max may vary, you
// want these to be as small/large as possible without hitting the hard stop
// for max range. You'll have to tweak them as necessary to match the servos you
// have!
#define SERVOMIN 125
#define SERVOMAX 625
#define SERVOMID 375
#define SERVOMIN_3 240
#define SERVOMAX_3 510
#define SERVOMIN_12 300
#define SERVOMAX_12 450
#define SERVOMIN_7 280
#define SERVOMAX_7 470
uint16_t pulselen_0=SERVOMID;
float pulselen_1=SERVOMID;
float pulselen_2=SERVOMID;
uint16_t pulselen_3=SERVOMID;
uint16_t pulselen_4=SERVOMID;
uint16_t pulselen_5=SERVOMID;
uint16_t pulselen_6=SERVOMID;
uint16_t pulselen_7=SERVOMID;
uint16_t angle=0;
float angle1=0;
bool pt0=0;
bool pt1=0;
bool pt2=0;
bool pt3=0;
bool pt4=0;
bool pt5=0;
bool pt6=0;
// our servo # counter
//uint8_t servonum = 0;
// you can use this function if you'd like to set the pulse length in seconds
// e.g. setServoPulse(0, 0.001) is a ~1 millisecond pulse width. its not precise!
void setServoPulse(uint8_t n, double pulse) {
double pulselength;
//float pulselen;
pulselength = 1000000; // 1,000,000 us per second
pulselength /= 60; // 60 Hz
Serial.print(pulselength); Serial.println(" us per period");
pulselength /= 4096; // 12 bits of resolution
Serial.print(pulselength); Serial.println(" us per bit");
pulse *= 1000;
pulse /= pulselength;
Serial.println(pulse);
pwm.setPWM(n, 0, pulse);
}
void Servo0(uint16_t pulselen,int i){
angle=pulselen-1;
for(;pulselen>angle;pulselen=pulselen-1){
pwm.setPWM(i,0,pulselen);
}
}
void Servo1(uint16_t pulselen,int i){
angle=pulselen+1;
for(;pulselen<angle;pulselen=pulselen+1){
pwm.setPWM(i,0,pulselen);
}
}
void Servo1_12(float pulselen,int i){
angle1=pulselen+0.5;
for(;pulselen<angle1;pulselen=pulselen+0.5){
pwm.setPWM(i,0,pulselen);
}
}
void Servo0_12(float pulselen,int i){
angle1=pulselen-0.5;
for(;pulselen>angle1;pulselen=pulselen-0.5){
pwm.setPWM(i,0,pulselen);
}
}
/******************************************************************
* set pins connected to PS2 controller:
* - 1e column: original
* - 2e colmun: Stef?
* replace pin numbers by the ones you use
******************************************************************/
//PS2手柄引脚;
#define PS2_DAT 13 //14
#define PS2_CMD 11 //15
#define PS2_SEL 10 //16
#define PS2_CLK 12 //17
// 电机控制引脚;
#define DIRA 8
#define DIRB 7
//PWM控制引脚;
#define PWMA 9
#define PWMB 6
int speed;
/******************************************************************
* select modes of PS2 controller:
* - pressures = analog reading of push-butttons
* - rumble = motor rumbling
* uncomment 1 of the lines for each mode selection
******************************************************************/
#define pressures true
//#define pressures false
#define rumble true
//#define rumble false
void (* resetFunc) (void) = 0;
PS2X ps2x; // create PS2 Controller Class
//right now, the library does NOT support hot pluggable controllers, meaning
//you must always either restart your Arduino after you connect the controller,
//or call config_gamepad(pins) again after connecting the controller.
int error = 0;
byte type = 0;
byte vibrate = 0;
void setup(){
pinMode(DIRA, OUTPUT);
pinMode(DIRB, OUTPUT);
pinMode(PWMA, OUTPUT);
pinMode(PWMB, OUTPUT);
Serial.begin(115200);
//double val=ps2x.read_gamepad();
delay(500) ; //added delay to give wireless ps2 module some time to startup, before configuring it
//CHANGES for v1.6 HERE!!! **************PAY ATTENTION*************
//setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);
if(error == 0){
Serial.print("Found Controller, configured successful ");
Serial.print("pressures = ");
if (pressures)
Serial.println("true ");
else
Serial.println("false");
Serial.print("rumble = ");
if (rumble)
Serial.println("true)");
else
Serial.println("false");
Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;");
Serial.println("holding L1 or R1 will print out the analog stick values.");
Serial.println("Note: Go to www.billporter.info for updates and to report bugs.");
}
else if(error == 1)
{
Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");
resetFunc();
}
else if(error == 2)
Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");
else if(error == 3)
Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
type = ps2x.readType();
switch(type) {
case 0:
Serial.println("Unknown Controller type found ");
break;
case 1:
Serial.println("DualShock Controller found ");
break;
case 2:
Serial.println("GuitarHero Controller found ");
break;
case 3:
Serial.println("Wireless Sony DualShock Controller found ");
break;
}
Serial.println("16 channel Servo test!");
pwm.begin();
pwm.setPWMFreq(60);
pwm.setPWM(0,0,SERVOMID);
pwm.setPWM(1,0,SERVOMID);
pwm.setPWM(3,0,SERVOMID);
pwm.setPWM(2,0,SERVOMID);
pwm.setPWM(4,0,SERVOMID);
pwm.setPWM(5,0,SERVOMID);
pwm.setPWM(6,0,SERVOMID);
pwm.setPWM(7,0,SERVOMID);
}
//定义小车运动方式
void turnLeft(int speed)//左转
{
digitalWrite(DIRA,HIGH);
digitalWrite(DIRB,HIGH);
analogWrite(PWMA, speed);
analogWrite(PWMB, speed);
}
void turnRight(int speed)//右转
{
digitalWrite(DIRA,LOW);
digitalWrite(DIRB,LOW);
analogWrite(PWMA, speed);
analogWrite(PWMB, speed);
}
void forward(int speed)//前进
{
digitalWrite(DIRA,LOW);
digitalWrite(DIRB,HIGH);
analogWrite(PWMA, speed);
analogWrite(PWMB, speed);
}
void back(int speed)//后退
{
digitalWrite(DIRA,HIGH);
digitalWrite(DIRB,LOW);
analogWrite(PWMA, speed);
analogWrite(PWMB, speed);
}
void stop() // 停止;
{
digitalWrite(DIRA,LOW);
digitalWrite(DIRB,LOW);
analogWrite(PWMA, 0);
analogWrite(PWMB, 0);
delay(20);
}
void loop(){
/* You must Read Gamepad to get new values and set vibration values
ps2x.read_gamepad(small motor on/off, larger motor strenght from 0-255)
if you don't enable the rumble, use ps2x.read_gamepad(); with no values
You should call this at least once a second
*/
if(error == 1) //skip loop if no controller found
return;
if(type == 2) {//Guitar Hero Controller
return;
}
else { //DualShock Controller
ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed
vibrate = ps2x.Analog(PSAB_CROSS); //this will set the large motor vibrate speed based on how hard you press the blue (X) button
if (ps2x.NewButtonState()) { //will be TRUE if any button changes state (on to off, or off to on)
if(ps2x.Button(PSB_L2)){
Serial.println("L2 pressed");
pt0=0;
pt1=0;
pt2=0;
pt3=0;
pt4=0;
pt5=1;
pt6=0;
}
if(ps2x.Button(PSB_R2)){
Serial.println("R2 pressed");
pt0=0;
pt1=0;
pt2=0;
pt3=0;
pt4=0;
pt5=0;
pt6=1;
}
if(ps2x.Button(PSB_TRIANGLE)){
Serial.println("Triangle pressed");
pt0=0;
pt1=1;
pt2=0;
pt3=0;
pt4=0;
pt5=0;
pt6=0;
}
}
if(ps2x.ButtonPressed(PSB_CIRCLE)){ //will be TRUE if button was JUST pressed
Serial.println("Circle just pressed");
pt0=0;
pt1=0;
pt2=0;
pt3=1;
pt4=0;
pt5=0;
pt6=0;
}
if(ps2x.NewButtonState(PSB_CROSS)){ //will be TRUE if button was JUST pressed OR released
Serial.println("X just changed");
pt0=0;
pt1=0;
pt2=0;
pt3=0;
pt4=1;
pt5=0;
pt6=0;
}
if(ps2x.ButtonReleased(PSB_SQUARE)){ //will be TRUE if button was JUST released
Serial.println("Square just released");
pt0=0;
pt1=0;
pt2=1;
pt3=0;
pt4=0;
pt5=0;
pt6=0;
}
//start 开始运行,电机初PWM为100;
if(ps2x.Button(PSB_START)) {
Serial.println("Start is being held");
pt0=1;
pt1=0;
pt2=0;
pt3=0;
pt4=0;
pt5=0;
pt6=0;
}
// 电机正转;
if(ps2x.Button(PSB_PAD_UP)){
Serial.println("Up held this hard: ");
speed= 200;
forward(speed);
}
// 电机反转;
if(ps2x.Button(PSB_PAD_DOWN)){
Serial.print("Down held this hard: ");
speed= 150;
back(speed);
}
//左转;
if(ps2x.Button(PSB_PAD_LEFT)){
Serial.println("turn left ");
speed=50;
turnLeft(speed);
}
//右转;
if(ps2x.Button(PSB_PAD_RIGHT)){
Serial.println("turn right");
speed=50;
turnRight(speed);
}
// Stop
if(ps2x.Button(PSB_SELECT)){
Serial.println("stop");
speed = 0;
stop();
}
delay(20);
}
if(ps2x.Button(PSB_L1)||ps2x.Button(PSB_R1)){
int LY=ps2x.Analog(PSS_LY);
int LX=ps2x.Analog(PSS_LX);
int RY=ps2x.Analog(PSS_RY);
int RX=ps2x.Analog(PSS_RX);
if(pt0){
if((RY<100)&&(pulselen_0>=SERVOMIN)){//
Servo0(pulselen_0,0);
pulselen_0=pulselen_0-1;
}
else if((RY>=150)&&(pulselen_0<=SERVOMAX)){//
Servo1(pulselen_0,0);
pulselen_0=pulselen_0+1;
}
else{
pwm.setPWM(0,0,pulselen_0);
}
}
if(pt1){
if((RY<100)&&(pulselen_1>=SERVOMIN_12)){//
Servo0_12(pulselen_1,1);
pulselen_1=pulselen_1-0.5;
}
else if((RY>=150)&&(pulselen_1<=SERVOMAX_12)){//
Servo1_12(pulselen_1,1);
pulselen_1=pulselen_1+0.5;
}
else{
pwm.setPWM(1,0,pulselen_1);
}
}
if(pt1){
if((RY<100)&&(pulselen_2<=SERVOMAX_12)){//
Servo1_12(pulselen_2,2);
pulselen_2=pulselen_2+0.5;
Serial.println("pulselen_2");
Serial.println(pulselen_2);
}
else if((RY>=150)&&(pulselen_2>=SERVOMIN_12)){//
Servo0_12(pulselen_2,2);
pulselen_2=pulselen_2-0.5;
}
else{
pwm.setPWM(2,0,pulselen_2);
}
}
if(pt2){
if((RY<100)&&(pulselen_3>=SERVOMIN_3)){//
Servo0(pulselen_3,3);
pulselen_3=pulselen_3-1;
}
else if((RY>=150)&&(pulselen_3<=SERVOMAX_3)){//
Servo1(pulselen_3,3);
pulselen_3=pulselen_3+1;
}
else{
pwm.setPWM(3,0,pulselen_3);
}
}
if(pt3){
if((RY<100)&&(pulselen_4>=SERVOMIN)){//
Servo0(pulselen_4,4);
pulselen_4=pulselen_4-1;
}
else if((RY>=150)&&(pulselen_4<=SERVOMAX)){//
Servo1(pulselen_4,4);
pulselen_4=pulselen_4+1;
}
else{
pwm.setPWM(4,0,pulselen_4);
}
}
if(pt4){
if((RY<100)&&(pulselen_5>=SERVOMIN)){//
Servo0(pulselen_5,5);
pulselen_5=pulselen_5-1;
}
else if((RY>=150)&&(pulselen_5<=SERVOMAX)){//
Servo1(pulselen_5,5);
pulselen_5=pulselen_5+1;
}
else{
pwm.setPWM(5,0,pulselen_5);
}
}
if(pt5){
if((RY<100)&&(pulselen_6>=SERVOMIN)){//
Servo0(pulselen_6,6);
pulselen_6=pulselen_6-1;
}
else if((RY>=150)&&(pulselen_6<=SERVOMAX)){//
Servo1(pulselen_6,6);
pulselen_6=pulselen_6+1;
}
else{
pwm.setPWM(6,0,pulselen_6);
}
}
if(pt6){
if((RY<100)&&(pulselen_7>=SERVOMIN_7)){//
Servo0(pulselen_7,7);
pulselen_7=pulselen_7-1;
}
else if((RY>=150)&&(pulselen_7<=SERVOMAX_7)){//
Servo1(pulselen_7,7);
pulselen_7=pulselen_7+1;
}
else{
pwm.setPWM(7,0,pulselen_7);
}
}
else{
pwm.setPWM(0,0,pulselen_0);
pwm.setPWM(1,0,pulselen_1);
pwm.setPWM(2,0,pulselen_2);
pwm.setPWM(3,0,pulselen_3);
pwm.setPWM(4,0,pulselen_4);
pwm.setPWM(5,0,pulselen_5);
pwm.setPWM(6,0,pulselen_6);
pwm.setPWM(7,0,pulselen_7);
}
}
delay(5);
}