-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcansat_ahrs.ino
131 lines (106 loc) · 3.24 KB
/
cansat_ahrs.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
#include "src/cansat_imu.h"
static CansatHwSerial _IMUport(Serial1);
CansatIMU<CansatHwSerial> cansatIMU(_IMUport);
void setupAHRS(){
cansatIMU.begin(115200);
cansatStatus=-1;
Serial.println(F("AHRS begin"));
}
void reconnectAhrs(){
cansatIMU.begin(115200);
cansatIMU.rx_empty();
}
#define PROCESSING
void updateAHRS(){
if(cansatIMU.read()){
#ifdef SERIAL_MONITOR
Serial.print("roll/pitch/yaw/ax/ay/ax/mx/my/mz");
for(int i=0;i<8;i++){
Serial.print(cansatIMU.imu_datas[i]);Serial.print(',');
}
Serial.println(cansatIMU.imu_datas[8]);
cansatIMU.getHeading();
Serial.print("heading: ");
Serial.println(cansatIMU.heading);
#endif
#ifdef PROCESSING
Serial.print("%,1,6,"); // header,class,num data
Serial.print(cansatIMU.imu_datas[0]);Serial.print(',');
Serial.print(cansatIMU.imu_datas[1]);Serial.print(',');
Serial.print(cansatIMU.imu_datas[2]);Serial.print(',');
Serial.print(cansatIMU.imu_datas[3]);Serial.print(',');
Serial.print(cansatIMU.imu_datas[4]);Serial.print(',');
Serial.print(cansatIMU.imu_datas[5]);Serial.print(',');
Serial.print(calMotionAcceleration());Serial.print(',');
Serial.println(averageMotionAcc(calMotionAcceleration()));
#endif
}
}
void cansatStateM(int status){
switch(status){
case SETUP_COMPLETED :
Serial.println(F("Setup Completed"));
cansatStatus=READY_FOR_LAUNCH;
break;
case READY_FOR_LAUNCH :
Serial.println(F("Ready For Launch"));
cansatStatus=LAUNCH_DETECTED;
break;
case LAUNCH_DETECTED :
cansatStatus=FREEFALL_DETECTED;
Serial.println(F("Launch detected"));
break;
case FREEFALL_DETECTED :
cansatStatus=PARACHUTE_DEVELPED;
Serial.println(F("Freefall detected"));
break;
case PARACHUTE_DEVELPED :
cansatStatus=AUTOPILOT_ACTIVATED;
Serial.println(F("Parachte developed"));
break;
case AUTOPILOT_ACTIVATED :
//cansatStatus=LANDING;
Serial.println(F("Autopilot activated"));
break;
case TURN_AROUND :
//cansatStatus=AUTOPILOT_ACTIVATED;
Serial.println(F("Autopilot activated"));
break;
case LANDING :
Serial.println(F("Landing"));
break;
default:
break;
}
}
void freeFallDetection(){
float motionAcceleration=averageMotionAcc(calMotionAcceleration());
if (motionAcceleration<0.3){
Serial.println(F("Freefall detected"));
//schedule_timer.setTimeout(7000, setAutoMode);
}
}
void setAutoMode(){
setNavigationMode(0);
}
float calMotionAcceleration(){
float ax=cansatIMU.imu_datas[3];
float ay=cansatIMU.imu_datas[4];
float az=cansatIMU.imu_datas[5];
return pow( (ax*ax+ay*ay+az*az),0.5);
}
#define MA_NBUF 3
float MotionAccBuf[MA_NBUF]={1,};
int ma_offset=0;
float averageMotionAcc(float motion_acc){
float v_sum;
// buffering V_NBUF data
MotionAccBuf[ma_offset]=motion_acc;
ma_offset++;
ma_offset %= MA_NBUF;
//calculate average
v_sum=0;
for(int i=0;i<MA_NBUF;i++) v_sum += MotionAccBuf[i];
float avg_v=v_sum/float(MA_NBUF);
return avg_v;
}