forked from BitsRobocon/IRC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ManualBot1.ino
215 lines (186 loc) · 4.73 KB
/
ManualBot1.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
#include <XBOXUSB.h>
#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif
#include <SPI.h>
#include <Servo.h>
byte LF=18;//orange
byte LS=2; //yellow
byte LSB=3; //yellow-black
byte LB=17;//green
byte RF=15;//grey
byte RS=4;//purple
byte RSB=5;//purple-black
byte RB=14;//blue
byte shootF=22;//blue
byte shootS=24;//green
byte shootB=26;//yellow
byte aim=30;
int x=0,y=0,c=0,o=0;
int rs=0,ls=0;
int armd=0;int handd=150;int gripd=0;
USB Usb;
XBOXUSB Xbox(&Usb);
Servo arm1;
Servo arm2;
Servo hand1;
Servo hand2;
Servo grip;
Servo reload;
void setup() {
Serial.begin(115200);
#if !defined(__MIPSEL__)
while (!Serial);
#endif
if (Usb.Init() == -1) {
Serial.print(F("\r\nOSC did not start"));
while (1); //halt
}
arm1.attach(6);//right-increase is up
arm2.attach(21);
hand1.attach(7);//right-increase is up
hand2.attach(20);//left-increase is down
grip.attach(8);//increase is inside
reload.attach(19);
pinMode(shootF,OUTPUT);
pinMode(shootS,OUTPUT);
pinMode(shootB,OUTPUT);
pinMode(LF,OUTPUT);
pinMode(LS,OUTPUT);
pinMode(LSB,OUTPUT);
pinMode(LB,OUTPUT);
pinMode(RF,OUTPUT);
pinMode(RS,OUTPUT);
pinMode(RSB,OUTPUT);
pinMode(RB,OUTPUT);
pinMode(aim,OUTPUT);
arm1.write(armd);
arm2.write(180-armd);
hand1.write(handd);
hand2.write(180-handd);
grip.write(gripd);
}
void motors(){
if (Xbox.getAnalogHat(LeftHatX) != 0 || Xbox.getAnalogHat(LeftHatY) != 0) {
if (Xbox.getAnalogHat(LeftHatX) != 0)
x=map(Xbox.getAnalogHat(LeftHatX),-32512,32512,-50,50);
else x=0;
if (Xbox.getAnalogHat(LeftHatY) != 0)
y=map(Xbox.getAnalogHat(LeftHatY),-32512,32512,-200,200);
else y=0;
}
else
{
x=0;y=0;
}
rs=y-x;
ls=y+x;
if(rs>=0)
{
digitalWrite(RF,HIGH);
digitalWrite(RB,LOW);
if(rs<20){
analogWrite(RS,0);
analogWrite(RSB,0);
}else{
analogWrite(RS,map(rs,20,255,38,255));
analogWrite(RSB,rs);
}
}
if(rs<0)
{
digitalWrite(RF,LOW);
digitalWrite(RB,HIGH);
if(rs>-20){
analogWrite(RS,0);
analogWrite(RSB,0);
}else
analogWrite(RS,-map(rs,-255,-20,-255,-32));
analogWrite(RSB,(-rs));
}
if(ls>=0)
{
digitalWrite(LF,HIGH);
digitalWrite(LB,LOW);
if(ls<20){
analogWrite(LSB,0);
analogWrite(LS,0);
}
else
{
analogWrite(LSB,map(ls,20,255,22,255));
analogWrite(LS,ls);
}
}
if(ls<0)
{
digitalWrite(LF,LOW);
digitalWrite(LB,HIGH);
if(ls>-20){
analogWrite(LS,0);
analogWrite(LSB,0);
}
else
analogWrite(LSB,-map(ls,-255,-20,-255,-23));
analogWrite(LS,(-ls));
}
Serial.print(ls);
Serial.print(" ");
Serial.println(rs);
}
void servos()
{
if ((Xbox.getAnalogHat(RightHatX) != 0)||(Xbox.getAnalogHat(RightHatY) != 0)||(Xbox.getButtonPress(L2) !=0)||(Xbox.getButtonPress(R2) !=0)){
if (Xbox.getAnalogHat(RightHatX) != 0)
x=map(Xbox.getAnalogHat(RightHatX),-32512,32512,-3,3);
else x=0;
if (Xbox.getAnalogHat(RightHatY) != 0)
y=map(Xbox.getAnalogHat(RightHatY),-32512,32512,-2,2);
else y=0;
if(Xbox.getButtonPress(L2) !=0)
c=map(Xbox.getButtonPress(L2),0,255,0,3);
else c=0;
if(Xbox.getButtonPress(R2) !=0)
o=map(Xbox.getButtonPress(R2),0,255,0,3);
else o=0;
armd=armd+y;
handd=handd+x;
gripd=gripd+c-o;
armd=constrain(armd,0,90);
handd=constrain(handd,0,180);
gripd=constrain(gripd,0,180);
arm1.write(armd);
arm2.write(180-armd);
hand1.write(handd);
hand2.write(180-handd);
grip.write(gripd);
delay(50);
}
}
void loop() {
Usb.Task();
if (Xbox.Xbox360Connected) {
Xbox.setRumbleOn(0,0);
motors();
servos();
if (Xbox.getButtonClick(A)){
digitalWrite(shootF,HIGH);
digitalWrite(shootS,HIGH);
digitalWrite(shootB,LOW);
}
if(Xbox.getButtonClick(B)){
digitalWrite(shootF,LOW);
digitalWrite(shootS,LOW);
digitalWrite(shootB,LOW);
}
if (Xbox.getButtonClick(X))
reload.write(100);
if (Xbox.getButtonClick(Y))
reload.write(30);
if (Xbox.getButtonClick(L1))
digitalWrite(aim,HIGH);
if (Xbox.getButtonClick(R1))
digitalWrite(aim,LOW);
}
delay(5);
}