-
Notifications
You must be signed in to change notification settings - Fork 0
/
heli_control.c
261 lines (213 loc) · 6.9 KB
/
heli_control.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
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
/** @file heli_control.c
@author Nick Wareing
@date 12 August 2013
*/
#include "heli_control.h"
#include "queue_structs.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/sysctl.h"
#include "driverlib/adc.h"
#include "driverlib/gpio.h"
#include "driverlib/pwm.h"
#define UP 0
#define DOWN 1
#define INITIAL_ALT 20
static void vGetAltitude ( void *pvParameters );
static unsigned int getRawADC( void );
static unsigned int desiredAltitude = INITIAL_ALT;
xQueueHandle xButtonQueue;
xQueueHandle xAltQueue;
xSemaphoreHandle xBinarySelectSemaphore;
typedef struct
{
double error_integrated;
double error_previous ;
} pid_state_t ;
static pid_state_t APID;
/* The handler for the GPIOPortB Pin4, Pin5 & Pin6 state change interrupt. */
void portBIntHandler(void) {
//clear the interrupt
GPIOPinIntClear (GPIO_PORTB_BASE, (GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6));
int button;
//Read the state of the pins
//Select icon clicked to start/stop heli
if (!GPIOPinRead( GPIO_PORTB_BASE, GPIO_PIN_4 )) {
xSemaphoreGiveFromISR( xBinarySelectSemaphore, pdFALSE );
}
//Up icon clicked
if (!GPIOPinRead( GPIO_PORTB_BASE, GPIO_PIN_5 )) {
button = UP;
xQueueSendFromISR( xButtonQueue, &button, NULL );
}
//Down icon clicked
if (!GPIOPinRead( GPIO_PORTB_BASE, GPIO_PIN_6 )) {
button = DOWN;
xQueueSendFromISR( xButtonQueue, &button, NULL );
}
//Reset icon clicked
if (!GPIOPinRead( GPIO_PORTB_BASE, GPIO_PIN_1 )) {
SysCtlReset();
}
}
static unsigned int getRawADC( void ) {
unsigned int rawADCvalue;
unsigned long rawADCarray[1];
// Trigger the ADC conversion.
ADCProcessorTrigger(ADC0_BASE, 3);
//
// Wait for conversion to be completed.
while(!ADCIntStatus(ADC0_BASE, 3, false))
{
}
ADCIntClear(ADC0_BASE, 3);
// Read ADC Value.
ADCSequenceDataGet(ADC0_BASE, 3, rawADCarray);
rawADCvalue = rawADCarray[0];
return rawADCvalue;
}
static void pid_init ()
{
APID.error_integrated = 0.0;
APID.error_previous = 0.0;
}
static float pid_update (float error, double proportional_gain, double integral_gain, double derivative_gain, double delta_t)
{
float error_derivative;
float control;
APID.error_integrated += error * delta_t;
error_derivative = (error - APID.error_previous)/ delta_t;
control = error * proportional_gain + APID.error_integrated * integral_gain + error_derivative * derivative_gain;
APID.error_previous = error;
//Limit the value to the duty cycle limits.
if (control > 95){
control = 95;
}
if (control < 5){
control = 5;
}
return control;
}
/* Control the flight of the helicopter. */
static void vPIDControlHeli( void *pvParameters)
{
//Set the altitude gains.
static const float Aki = 0.4, Akp = 3.0, Akd = 0.25;
static const float delta_t = 0.02;
float altError, altResponse;
float currentAltitude = 0;
portBASE_TYPE xStatus;
int buttonMessage;
xAltMessage altMessage;
xQueueHandle xOLEDQueue = ( xQueueHandle ) pvParameters;
portTickType xLastWakeTime = xTaskGetTickCount();
/* Define a period of 2 milliseconds (500Hz) */
const portTickType xPeriod = ( 3 / portTICK_RATE_MS );
for ( ;; ) {
vTaskDelayUntil( &xLastWakeTime, xPeriod );
// Act on button presses sent from the ISR.
xStatus = xQueueReceive( xButtonQueue, &buttonMessage, 0 );
if (xStatus == pdPASS)
{
switch (buttonMessage)
{
case UP:
if (desiredAltitude <= 80)
desiredAltitude += 10;
break;
case DOWN:
if (desiredAltitude >= 10)
desiredAltitude -= 10;
break;
}
xQueueMessage message = { DESIRED_ALTITUDE, desiredAltitude };
xQueueSendToBack( xOLEDQueue, &message, 0 );
}
// Recompute the PID output using the latest altitude value.
xStatus = xQueueReceive( xAltQueue, &altMessage, 0 );
if (xStatus == pdPASS)
{
currentAltitude = altMessage.pcMessage;
altError = desiredAltitude - currentAltitude;
altResponse = pid_update(altError, Akp, Aki, Akd, delta_t);
PWMPulseWidthSet(PWM_BASE, PWM_OUT_1, ( SysCtlClockGet() / 150 ) * altResponse / 100);
}
}
}
/* Poll the ADC for altitude values and put the
* latest values on the altitude queue.
*/
static void vGetAltitude(void *pvParameters) {
unsigned int groundValue = getRawADC();
unsigned int maxValue = groundValue - 270;
unsigned int rawADCvalue;
float altitude;
xQueueHandle xOLEDQueue = ( xQueueHandle ) pvParameters;
portTickType xLastWakeTime = xTaskGetTickCount();
/* Define a period of 2 milliseconds (500Hz) */
const portTickType xPeriod = ( 3 / portTICK_RATE_MS );
for ( ;; ) {
vTaskDelayUntil( &xLastWakeTime, xPeriod );
rawADCvalue = getRawADC();
//Dynamically adjust the ground voltage value (differs on each helicopter).
if (rawADCvalue > groundValue) {
groundValue = rawADCvalue;
}
//Dynamically adjust the max altitude voltage value.
if (rawADCvalue < (groundValue - 320)) {
rawADCvalue = maxValue;
}
//Convert the voltage values to a percentage of the total height.
altitude = 100.0f * (groundValue - rawADCvalue) / (groundValue - maxValue);
/* Uncomment to send the current altitude to the OLED */
//xQueueMessage message = { CURRENT_ALTITUDE, altitude };
//xQueueSendToBack( xOLEDQueue, &message, 0 );
xAltMessage altMessage = { altitude };
xQueueSendToBack( xAltQueue, &altMessage, 0 );
}
}
/* Act on select button pushes received from the ISR.
* Toggle the helicopter rotor on and off with each push.
*/
static void vStartStopPWM ( void *pvParameters )
{
/* Take the semaphore once to start with so the semaphore is empty before the
infinite loop is entered. The semaphore was created before the scheduler
was started so before this task ran for the first time.*/
xSemaphoreTake( xBinarySelectSemaphore, 0 );
portBASE_TYPE flying = pdFALSE;
for( ;; )
{
/* Use the semaphore to wait for the event. */
xSemaphoreTake( xBinarySelectSemaphore, portMAX_DELAY );
if (flying == pdFALSE) {
pid_init();
// Turn the main rotor on.
PWMOutputState(PWM_BASE, PWM_OUT_1_BIT, true);
// Reset the desired height
desiredAltitude = INITIAL_ALT;
flying = pdTRUE;
} else {
// Turn the thing off.
while (desiredAltitude > 5) {
desiredAltitude -= 2;
vTaskDelay( 100 / portTICK_RATE_MS );
}
PWMOutputState(PWM_BASE, PWM_OUT_1_BIT, false);
flying = pdFALSE;
}
}
}
void vCreateQueuesAndSemaphore( void )
{
vSemaphoreCreateBinary( xBinarySelectSemaphore );
xButtonQueue = xQueueCreate( 1, sizeof( int ) );
xAltQueue = xQueueCreate( 3, sizeof( xAltMessage ) );
}
void vStartControlTasks( xQueueHandle xOLEDQueue )
{
xTaskCreate( vPIDControlHeli, "PID Control", 240, (void *) xOLEDQueue, 2, NULL );
xTaskCreate( vGetAltitude, "Get Altitude", 240, (void *) xOLEDQueue, 2, NULL );
xTaskCreate( vStartStopPWM, "Start/Stop Heli", 180, NULL, 5, NULL );
}