-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch_oct05a.ino
231 lines (197 loc) · 6.14 KB
/
sketch_oct05a.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
#include <PsxControllerBitBang.h>
#include <DigitalIO.h>
#include <AccelStepper.h>
#include <Servo.h>
#include <avr/pgmspace.h>
typedef const __FlashStringHelper * FlashStr;
typedef const byte* PGM_BYTES_P;
#define PSTR_TO_F(s) reinterpret_cast<const __FlashStringHelper *> (s)
#define X_STEP_PIN 2
#define X_DIR_PIN 5
//#define X_MIN_PIN A0
#define X_MAX_PIN 9
#define Y_STEP_PIN 3
#define Y_DIR_PIN 6
//#define Y_MIN_PIN A2
#define Y_MAX_PIN 10
#define Z_STEP_PIN 2
#define Z_DIR_PIN 5
//#define Z_MIN_PIN A4
#define Z_MAX_PIN 11
#define PIN_PS2_ATT A3
#define PIN_PS2_DAT 13
#define PIN_PS2_CMD 12
#define PIN_PS2_CLK A0
#define PIN_PS2_ACK A1
//#define SERVO_1_PIN 12
AccelStepper stepper1(1, X_STEP_PIN, X_DIR_PIN);
AccelStepper stepper2(1, Y_STEP_PIN, Y_DIR_PIN);
AccelStepper stepper3(1, Z_STEP_PIN, Z_DIR_PIN);
Servo clawServo;
const char buttonSelectName[] PROGMEM = "Select";
const char buttonL3Name[] PROGMEM = "L3";
const char buttonR3Name[] PROGMEM = "R3";
const char buttonStartName[] PROGMEM = "Start";
const char buttonUpName[] PROGMEM = "Up";
const char buttonRightName[] PROGMEM = "Right";
const char buttonDownName[] PROGMEM = "Down";
const char buttonLeftName[] PROGMEM = "Left";
const char buttonL2Name[] PROGMEM = "L2";
const char buttonR2Name[] PROGMEM = "R2";
const char buttonL1Name[] PROGMEM = "L1";
const char buttonR1Name[] PROGMEM = "R1";
const char buttonTriangleName[] PROGMEM = "Triangle";
const char buttonCircleName[] PROGMEM = "Circle";
const char buttonCrossName[] PROGMEM = "Cross";
const char buttonSquareName[] PROGMEM = "Square";
const int stepsPerRevolution = 200;
const char* const psxButtonNames[PSX_BUTTONS_NO] PROGMEM = {
buttonSelectName,
buttonL3Name,
buttonR3Name,
buttonStartName,
buttonUpName,
buttonRightName,
buttonDownName,
buttonLeftName,
buttonL2Name,
buttonR2Name,
buttonL1Name,
buttonR1Name,
buttonTriangleName,
buttonCircleName,
buttonCrossName,
buttonSquareName
};
byte psxButtonToIndex (PsxButtons psxButtons) {
byte i;
for (i = 0; i < PSX_BUTTONS_NO; ++i) {
if (psxButtons & 0x01) {
break;
}
psxButtons >>= 1U;
}
return i;
}
FlashStr getButtonName (PsxButtons psxButton) {
FlashStr ret = F("");
byte b = psxButtonToIndex (psxButton);
if (b < PSX_BUTTONS_NO) {
PGM_BYTES_P bName = reinterpret_cast<PGM_BYTES_P> (pgm_read_ptr (&(psxButtonNames[b])));
ret = PSTR_TO_F (bName);
}
return ret;
}
void dumpButtons (PsxButtons psxButtons) {
static PsxButtons lastB = 0;
if (psxButtons != lastB) {
lastB = psxButtons; // Save it before we alter it
Serial.print (F("Pressed: "));
for (byte i = 0; i < PSX_BUTTONS_NO; ++i) {
byte b = psxButtonToIndex (psxButtons);
if (b < PSX_BUTTONS_NO) {
PGM_BYTES_P bName = reinterpret_cast<PGM_BYTES_P> (pgm_read_ptr (&(psxButtonNames[b])));
Serial.print (PSTR_TO_F (bName));
}
psxButtons &= ~(1 << b);
if (psxButtons != 0) {
Serial.print (F(", "));
}
}
Serial.println ();
}
}
void dumpAnalog (const char *str, const byte x, const byte y) {
Serial.print (str);
Serial.print (F(" analog: x = "));
Serial.print (x);
Serial.print (F(", y = "));
Serial.println (y);
}
const char ctrlTypeUnknown[] PROGMEM = "Unknown";
const char ctrlTypeDualShock[] PROGMEM = "Dual Shock";
const char ctrlTypeDsWireless[] PROGMEM = "Dual Shock Wireless";
const char ctrlTypeGuitHero[] PROGMEM = "Guitar Hero";
const char ctrlTypeOutOfBounds[] PROGMEM = "(Out of bounds)";
const char* const controllerTypeStrings[PSCTRL_MAX + 1] PROGMEM = {
ctrlTypeUnknown,
ctrlTypeDualShock,
ctrlTypeDsWireless,
ctrlTypeGuitHero,
ctrlTypeOutOfBounds
};
PsxControllerBitBang<PIN_PS2_ATT, PIN_PS2_CMD, PIN_PS2_DAT, PIN_PS2_CLK> psx;
boolean haveController = true;
void setup() {
Serial.begin(115200);//sets the baud rate
Serial.println (F("Ready!"));
//clawServo.attach(SERVO_1_PIN);
pinMode(X_MAX_PIN, INPUT);
pinMode(Y_MAX_PIN, INPUT);
pinMode(Z_MAX_PIN, INPUT);
stepper1.setMaxSpeed(200);
stepper1.setAcceleration(100);
stepper2.setMaxSpeed(200);
stepper2.setAcceleration(100);
stepper3.setMaxSpeed(200);
stepper3.setAcceleration(100);
clawServo.write(90);
}
void loop() {
static byte slx, sly, srx, sry;
if (!haveController) {
if (psx.begin ()) {
Serial.println (F("Controller found!"));
delay (300);
if (!psx.enterConfigMode ()) {
Serial.println (F("Cannot enter config mode"));
} else {
PsxControllerType ctype = psx.getControllerType ();
PGM_BYTES_P cname = reinterpret_cast<PGM_BYTES_P> (pgm_read_ptr (&(controllerTypeStrings[ctype < PSCTRL_MAX ? static_cast<byte> (ctype) : PSCTRL_MAX])));
Serial.print (F("Controller Type is: "));
Serial.println (PSTR_TO_F (cname));
if (!psx.enableAnalogSticks ()) {
Serial.println (F("Cannot enable analog sticks"));
}
//~ if (!psx.setAnalogMode (false)) {
//~ Serial.println (F("Cannot disable analog mode"));
//~ }
//~ delay (10);
if (!psx.enableAnalogButtons ()) {
Serial.println (F("Cannot enable analog buttons"));
}
if (!psx.exitConfigMode ()) {
Serial.println (F("Cannot exit config mode"));
}
}
haveController = true;
}
}
else {
if (!psx.read ()) {
Serial.println (F("Controller lost :("));
haveController = false;
}
else {
dumpButtons (psx.getButtonWord ());
byte lx, ly;
psx.getLeftAnalog (lx, ly);
if (lx != slx || ly != sly) {
//dumpAnalog ("Left", lx, ly);
slx = lx;
sly = ly;
}
byte rx, ry;
psx.getRightAnalog (rx, ry);
if (rx != srx || ry != sry) {
dumpAnalog ("Right", rx, ry);
srx = rx;
sry = ry;
}
}
}
int speed1 = map(slx, 0, 255, 100, -100);
stepper1.setSpeed(speed1);
stepper1.runSpeed();
delay (1000 / 60);
}