-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShwatt.c
166 lines (140 loc) · 4.43 KB
/
Shwatt.c
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
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "Calibration/Calibrate.h"
#include "DAQ/ShmittTrigger.h"
#include "Algorithms/Kalman.h"
#include "XBee/XBee.h"
#include "Math/FractSupport.h"
#include "DAQ/Clock.h"
#include "Core/ShwattGlobals.h" // all shared state information
volatile uint16_t rawXaxis; // store location of interrrupts
volatile uint16_t rawYrate;
volatile uint16_t rawZaxis;
uint16_t rawExtPhi;
uint16_t rawExtPhiDot;
uint16_t rawExtPhiError;
uint16_t rawExtPhiDotError;
volatile uint8_t cyclesSinceLastBroadcast; //count how many iterations we
// perform between measurements
void clockOverflowHandler(void)
{
if ( KalmanState &= ReadyForData )
AcquireData();
if (rawTimeMSB() % BroadcastEveryNthDaq == 0)
{
if (StatusPins & StatusPin1)
StatusPins &= ~(StatusPin1);
else
StatusPins |= StatusPin1;
BroadcastData();
cyclesSinceLastBroadcast = 0;
}
}
// called when a DAQ is complete (from ShwattDaq.c)
void ShwattDaqComplete(void)
{
// prevent collision when accessing data
// this shouldn't happen often. In in case it does,
// the DAQ will keep the current value (and time, imortantly)
// until the next
if (StatusPins & StatusPin0)
StatusPins &= ~(StatusPin0);
else
StatusPins |= StatusPin0;
if( !(KalmanState & ReadyForData) )
return;
// Check if we have new data
uint16_t timeElapsed = DataReady(); //returns zero if no new data
if (timeElapsed)
{
measures[xAxisIndex] = rbits(rawXaxis << ScaleMeasures);
measures[yRateIndex] = rbits(rawYrate << ScaleMeasures);
measures[zAxisIndex] = rbits(rawZaxis << ScaleMeasures);
// flag that we have new data
timeSinceLastMeasure = time(timeElapsed);
KalmanState |= NewDataBit;
ShmittTrigger();
// check if not moving. If so, manually set phiDot to zero
if(!isMoving())
{
// don't use the shmitt trigger if we aren't moving.
KalmanState &= ~(ShmittTriggered);
// don't let phi dot go crazy
state[phiDotIndex] = 0;
initializeStateErrors();
}
}
// right now, no external data
}
//void XbeeFrameHandler(uint8_t frame_buffer_head)
//{
//TODO
//}
void setup(void)
{
DDRB = MyPortBMask;
StatusPins = StatusPin1;
SetupADC(&rawXaxis, &rawZaxis, &rawYrate);
setupClock();
BeginSerial(BaudRate);
EnableRemoteShwattInput();
timeSinceLastMeasure = 0;
KalmanState = 0;
TriggerState = 0;
CalibrationState = 0;
state[thetaIndex] = InitialTheta;
state[phiIndex] = InitialPhi;
state[phiDotIndex] = InitialPhiDot;
crankLength = InitialCrank;
initializeStateErrors();
/*
SetBroadcastData( XBeeContains_ShwattStatus
| XBeeContains_Measures
| XBeeContains_MeasureNoise
| XBeeContains_MeasureBiases );
*/
#ifdef FakeConditions
DoFakeCalibSequence(&measureBias[yRateIndex], &measureNoise[yRateIndex],
&measureBias[xAxisIndex], &measureNoise[xAxisIndex],
&measureBias[zAxisIndex], &measureNoise[zAxisIndex],
&gravity, &gyroGain);
#else
DoCalibSequence(&measureBias[yRateIndex], &measureNoise[yRateIndex],
&measureBias[xAxisIndex], &measureNoise[xAxisIndex],
&measureBias[zAxisIndex], &measureNoise[zAxisIndex],
&gravity, &gyroGain);
#endif
measureNoise[triggerPhiIndex] = TrigPhiNoise;
measureNoise[triggerPhiDotIndex] = TrigPhiDotNoise;
BroadcastData();
/*
SetBroadcastData( XBeeContains_InterCom //interfoot communication
| XBeeContains_State
| XBeeContains_Performance
| XBeeContains_ShwattStatus
| XBeeContains_Measures
| XBeeContains_StateErrors
);
*/
SetBroadcastData( XBeeContains_InterCom );//interfoot communication
KalmanState |= ReadyForData;
//KalmanState |= CrankUpdateBit;
//KalmanState |= BiasesUpdateBit;
}
void loop(void)
{
cyclesSinceLastBroadcast++;
KalmanCheckForNewData();
KalmanUpdateXaxis();
KalmanCheckForNewData();
KalmanUpdateZaxis();
}
int main(void)
{
sei();
setup();
for (;;)
loop();
return 0;
}