-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransmitter_Xbee_due.ino
298 lines (287 loc) · 6.59 KB
/
Transmitter_Xbee_due.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
#include <EnableInterrupt.h>
#include <Servo.h>
#define SERIAL_PORT_SPEED 57600
#define RC_NUM_CHANNELS 4
#define RC_CH1 0
#define RC_CH2 1
#define RC_CH3 2
#define RC_CH4 3
#define RC_CH1_INPUT A0
#define RC_CH2_INPUT A1
#define RC_CH3_INPUT A2
#define RC_CH4_INPUT A3
uint16_t rc_values[RC_NUM_CHANNELS];
uint32_t rc_start[RC_NUM_CHANNELS];
volatile uint16_t rc_shared[RC_NUM_CHANNELS];
double throttle_ref;
double RPM_L;
Servo MotorL;
Servo MotorR;
Servo MotorB;
Servo Motor_yaw;
char a;
String b;
int count;
char arr=' ';
double data_0;
double data_1;
double data_2;
double data_3;
double roll, pitch, yaw, prev_yaw;
double sensor_x, sensor_y, sensor_z;
int data_count;
int accelero_count;
/*
double roll = 0; // IMU Roll Value (Feedback)
double pitch = 0; // IMU Pitch value (Feedback)
double yaw = 0; // IMU Yaw (Feedback)
float height = 0; // Lidar Height Value (Feedback)
float lati; // GPS Latitude Value (Feedback)
float longi; // GPS Longitude Value (Feedback)
float roll_prev = 0; // Record of Previous Value to prevent Garbage
float pitch_prev = 0;
float yaw_prev = 0;
float height_prev = 0;
int test_count = 0;
int timestart; // Time record for response time measurement
int timestop; // Time record for response time measurement
int kill = 1; // Kill=1 => maunual
float hover_rpm = 6000;//5840;
*/
void setup()
{
Serial.begin(9600);
Serial1.begin(115200);
Serial2.begin(57600);
MotorL.attach(4);
MotorR.attach(5);
MotorB.attach(6);
Motor_yaw.attach(8);
pinMode(RC_CH1_INPUT, INPUT);
pinMode(RC_CH2_INPUT, INPUT);
pinMode(RC_CH3_INPUT, INPUT);
pinMode(RC_CH4_INPUT, INPUT);
enableInterrupt(RC_CH1_INPUT, calc_ch1, CHANGE);
enableInterrupt(RC_CH2_INPUT, calc_ch2, CHANGE);
enableInterrupt(RC_CH3_INPUT, calc_ch3, CHANGE);
enableInterrupt(RC_CH4_INPUT, calc_ch4, CHANGE);
throttle_ref=0;
RPM_L=0;
count=0;
roll=0;
pitch=0;
yaw=0;
data_count=0;
data_0=0;
data_1=0;
data_2=0;
data_3=0;
accelero_count=0;
sensor_x=0;
sensor_y=0;
sensor_z=0;
prev_yaw=0;
}
void loop()
{
// rc_read_values();
get_accelero_values();
if (millis()%50==0)
{
/*
Serial.println(roll);
Serial.println(pitch);
Serial.println(yaw);
*/
Serial2.println("sensor_x");
Serial2.println(sensor_x);
Serial2.println("sensor_y");
Serial2.println(sensor_y);
Serial2.println("sensor_z");
Serial2.println(sensor_z);
Serial2.println("roll");
Serial2.println(roll);
Serial2.println("pitch");
Serial2.println(pitch);
Serial2.println("yaw");
Serial2.println(yaw);
//Serial2.println("Thrust_motors");
//Serial2.println(rc_values[RC_CH3]);
Serial.println(pitch);
}
/*
throttle_ref=map(rc_values[RC_CH3], 1050, 1850, 0, 255);
RPM_L = map(throttle_ref, 0, 255, 1000, 2000);
MotorL.writeMicroseconds(RPM_L);
MotorR.writeMicroseconds(RPM_L);
MotorB.writeMicroseconds(1000);
Motor_yaw.writeMicroseconds(1000);
Serial.println(RPM_L);
*/
//imu();
}
void rc_read_values() {
noInterrupts();
memcpy(rc_values, (const void *)rc_shared, sizeof(rc_shared));
interrupts();
}
void calc_input(uint8_t channel, uint8_t input_pin) {
if (digitalRead(input_pin) == HIGH) {
rc_start[channel] = micros();
} else {
uint16_t rc_compare = (uint16_t)(micros() - rc_start[channel]);
rc_shared[channel] = rc_compare;
}
}
void calc_ch1() { calc_input(RC_CH1, RC_CH1_INPUT); }
void calc_ch2() { calc_input(RC_CH2, RC_CH2_INPUT); }
void calc_ch3() { calc_input(RC_CH3, RC_CH3_INPUT); }
void calc_ch4() { calc_input(RC_CH4, RC_CH4_INPUT); }
/*
void imu() {
//int datalength = 20; //112-116 (5 Registers)
int i = -1;
char ch = 0;
int count = 0;
uint16_t temp;
uint8_t in[20] = {NULL}; // 's','n','p',PT,Address,Data,Checksum[1],Checksum[0]
//////////////////////////////Parsing Binary Data /////////////////////////////////////////
while(1)
{
if(Serial1.available()>0)
{
i = Serial1.read();
if(i == -1);
//Serial.println("No Data");
else
{
//Serial.println("SUCCESS");
ch = i;
//Serial.println(ch);
if(in[0] == 's' && in[1] == 'n' && in[2] =='p')
{
count = count+1;
in[count] = ch;
}
else if(ch == 's')
{
count = 0;
in[count] = 's';
}
else if(ch == 'n')
{
count = 1;
in[count] = 'n';
}
else if(ch == 'p')
{
count = 2;
in[count] = 'p';
}
}
if(count == 17)
{
count = 0;
if(in[4] == 112)
{
temp = in[5] * 256 + in[6];
roll = ((int16_t) temp) / 91.02222;
//Serial.print("Roll :");
//Serial.print(roll);
//Serial.print(" ");
temp = in[7] * 256 + in[8];
pitch = ((int16_t) temp) / 91.02222 ;
//Serial.print("pitch :");
//Serial.print(pitch);
//Serial.print(" ");
temp = in[9] * 256 + in[10];
yaw = ((int16_t) temp) / 91.02222;
//Serial.print("yaw :");
//Serial.println(yaw);
break; // Break if Roll,Pitch,Yaw Acquired
}
for(i=0 ; i<20 ; i++)
in[i] = NULL;
}
}
}
}
*/
void get_accelero_values()
{
if (Serial1.available())
{
a=Serial1.read();
if (count==0)
{
if (a=='$')
{
count=1;
}
}
if (count == 1)
{
if (a==',')
{
data_count++;
if (data_count==3)
{
data_0= b.toDouble();
}
if (data_count==4)
{
data_1= b.toDouble();
//Serial.println(data);
}
if (data_count==5)
{
data_2= b.toDouble();
}
if (data_count==6)
{
data_3= b.toDouble();
}
b="";
// Serial.println(data_count);
}
if (a!=',' && data_count > 1 )
{
b+=a;
}
if (a=='*') // Check Sum
{
if (data_count==5)
{
roll= data_0;
pitch= data_1;
yaw= data_2;
}
if (data_count==6)
{
if (accelero_count<1)
{
// Serial.println(data_1);
sensor_x= data_1;
sensor_y= data_2;
sensor_z= data_3;
}
accelero_count++;
if (accelero_count>2)
{
accelero_count=0;
}
}
count=0;
b="";
data_count=0;
data_1=0;
data_2=0;
data_3=0;
}
}
//Serial.println(roll);
//Serial.println(test);
}
//Serial.println(b);
//Serial.println(roll);
}