-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgimbalcontroller.ino
246 lines (203 loc) · 6.58 KB
/
gimbalcontroller.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
/* Gimbal Controller
by Brett James / SuperRoach (@SuperRoach on twitter)
This code is in the public domain.
http://www.arduino.cc/en/Tutorial/Sweep
*/
#include <Servo.h>
Servo modecontrol, pitch, yaw; // create servo object to control a servo
/*
// Arduino Nano
int potPin = 2; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int potVal = 0; // variable to store the value coming from the sensor
*/
// Arduino Mini Pro
// The board I'm using has a spare breakout for A6/A7 at the rear. these are analog input only,
// Which is okay but it means I can't use one of the pins as power.
// I need to emulate through a digital pin elsewhere
int potPin = A6; // select the input pin for the potentiometer
int potPower = 9; // anything that can output a high digital signal.
int ledPin = 13; // select the pin for the LED
int potVal = 0; // variable to store the value coming from the sensor
int buttonPin = 8; // Push button
int buttonGnd = 7; // if needed
int buttonState = 0;
int servoMap = 0;
int previousServoMap = 0;
int modePin = 3;
int pitchPin = 5; // Blue Cable
int yawPin = 6; // White Cable
int gimbalModeState = 0;
String potControllerState = "pitch";
// Debounce stuff
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin
int state = HIGH; // the current state of the output pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long mydebounce = 200; // the debounce time, increase if the output flickers
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(A6, INPUT);
pinMode(potPower, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buttonGnd, OUTPUT);
digitalWrite(buttonGnd, LOW);
digitalWrite(potPower, HIGH);
//modecontrol.attach(modePin); // attaches the Brown and Black cable (number is for brown one)
//modecontrol.write( setControlMode( 2 ) );
// pitch
pitch.attach(pitchPin); // attaches the servo on pin 9 to the servo object
pitch.write(110);
// yaw
// 90 - infront
// 150 - Max Left
// yaw.attach(6);
// yaw.write(0);
}
void loop() {
/*
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
*/
// Stock: Mode 1, Switch on: Mode 2: Knob controls Pitch
// option: double switch hits mode 3?? tricky to do.
previousServoMap = servoMap;
int b = checkButton();
//Serial.println(b);
buttonState = digitalRead(buttonPin);
potVal = analogRead(potPin); // read the value from the sensor
if (reading == HIGH && previous == LOW && millis() - time > mydebounce) {
if (state == HIGH)
buttonState = LOW;
else
buttonState = HIGH;
time = millis();
}
previous = reading;
if (buttonState == LOW) {
/*
if (gimbalModeState != 2 ) {
modecontrol.write( setControlMode( 3 ) );
//gimbalModeState = 2;
// Serial.println("mode two");
}
*/
} else {
/* if (gimbalModeState != 1 ) {
modecontrol.write( setControlMode( 1 ) );
//gimbalModeState = 1;
// Serial.println("mode one");
}
*/
}
delay(100);
//digitalWrite(ledPin, HIGH); // turn the ledPin on
//delay(potVal); // stop the program for some time
//digitalWrite(ledPin, LOW); // turn the ledPin off
//delay(potVal); // stop the program for some time
// Control the Gimbal Mode
if (b == 2) { // Double Click
digitalWrite(ledPin, HIGH); // turn the ledPin on
if (potControllerState == "pitch")
{ potControllerState = "yaw";
Serial.println("Long Press Activated, setting Pot control to Yaw" );
delay(40);
digitalWrite(ledPin, LOW); // turn the ledPin on
delay(75);
digitalWrite(ledPin, HIGH); // turn the ledPin on
delay(40);
} else
{ potControllerState = "pitch";
Serial.println("Long Press Activated, setting Pot control to Pitch" );
delay(150);
digitalWrite(ledPin, LOW); // turn the ledPin on
delay(75);
digitalWrite(ledPin, HIGH); // turn the ledPin on
delay(150);
}
digitalWrite(ledPin, LOW); // turn the ledPin on
}
if (b == 4) // Very Long Press
{
if ( !modecontrol.attached() )
{ modecontrol.attach(modePin);
Serial.println("Somethings wrong, attaching");
}
if (gimbalModeState == 3) {
gimbalModeState = 0;
}
gimbalModeState++;
modecontrol.write( setControlMode( gimbalModeState ) );
Serial.println("very long press");
digitalWrite(ledPin, HIGH); // turn the ledPin on
delay(400);
for (int i=1; i <= gimbalModeState; i++)
{delay(100);
digitalWrite(ledPin, LOW); // turn the ledPin on
delay(250);
digitalWrite(ledPin, HIGH); // turn the ledPin on
delay(100);
digitalWrite(ledPin, LOW); // turn the ledPin on
}
Serial.println("well its done for");
}
// Set the servo/pwm value
//Serial.println(previousServoMap);
servoMap = map(potVal, 0, 1023, 0, 180);
if (previousServoMap != servoMap)
{
if ( !pitch.attached() )
{ pitch.attach(pitchPin);
Serial.println("Somethings wrong");
}
Serial.println("changing it");
if (potControllerState == "pitch" )
{
pitch.write(servoMap);
} else
{
if ( !yaw.attached() )
{ yaw.attach(yawPin);
Serial.println("Yaw Active");
}
yaw.write(servoMap);
}
Serial.println(servoMap);
}
else
{
// pitch.detach();
}
}
int setControlMode(int x){
// Set the mode up for the three different states via PWM channel.
// Mode 1: "HEADING FOLLOW" Standard. Looks forward to the front. YAW: Locked, PITCH: Controllable
// Mode 2: "HEADING AND PITCH FOLLOW" Yaw is not automated. YAW: Controllable, PITCH: Controllable
// Mode 3: "HEADING LOCK" All Axis are automated YAW: Locked, PITCH: Locaked
int result;
switch (x) {
case 1:
result = 0;
break;
case 2:
result = 45;
break;
case 3:
result = 90;
break;
default:
result = 0;
break;
}
return result;
}