From e2c8ffee5ada53e1f08253f2dd82e2e55985ceda Mon Sep 17 00:00:00 2001 From: Lennart Eriksson Date: Wed, 23 Nov 2016 13:56:55 +0100 Subject: [PATCH] Got compass to work again --- UAV-ControlSystem/inc/Flight/pid.h | 2 + UAV-ControlSystem/inc/Scheduler/scheduler.h | 1 + UAV-ControlSystem/inc/Scheduler/tasks.h | 1 + UAV-ControlSystem/inc/config/eeprom.h | 1 + UAV-ControlSystem/inc/system_variables.h | 2 +- UAV-ControlSystem/src/Flight/pid.c | 75 +++++++++++++++++++-- UAV-ControlSystem/src/Scheduler/scheduler.c | 2 + UAV-ControlSystem/src/Scheduler/tasks.c | 8 +++ UAV-ControlSystem/src/config/cli.c | 5 ++ UAV-ControlSystem/src/config/eeprom.c | 6 ++ UAV-ControlSystem/src/drivers/arduino_com.c | 22 +++--- UAV-ControlSystem/src/drivers/usart.c | 2 +- UAV-ControlSystem/src/main.c | 6 +- UAV-ControlSystem/src/tasks_main.c | 7 ++ 14 files changed, 121 insertions(+), 19 deletions(-) diff --git a/UAV-ControlSystem/inc/Flight/pid.h b/UAV-ControlSystem/inc/Flight/pid.h index 7213f7d..ee9b57e 100644 --- a/UAV-ControlSystem/inc/Flight/pid.h +++ b/UAV-ControlSystem/inc/Flight/pid.h @@ -67,6 +67,8 @@ extern float accPitchFineTune; extern accel_t accelProfile; /*Struct profile for input data from sensor*/ +extern float Yaw; +extern float YawU; /************************************************************************** diff --git a/UAV-ControlSystem/inc/Scheduler/scheduler.h b/UAV-ControlSystem/inc/Scheduler/scheduler.h index 945f953..c1dae17 100644 --- a/UAV-ControlSystem/inc/Scheduler/scheduler.h +++ b/UAV-ControlSystem/inc/Scheduler/scheduler.h @@ -107,6 +107,7 @@ typedef enum TASK_RX_CLI, TASK_SERIAL, TASK_BATTERY, + TASK_ARDUINO, #ifdef BARO TASK_BARO, #endif diff --git a/UAV-ControlSystem/inc/Scheduler/tasks.h b/UAV-ControlSystem/inc/Scheduler/tasks.h index 2a6c79c..cbe0756 100644 --- a/UAV-ControlSystem/inc/Scheduler/tasks.h +++ b/UAV-ControlSystem/inc/Scheduler/tasks.h @@ -38,6 +38,7 @@ void systemTaskRxCli(void); bool systemTaskRxCliCheck(uint32_t currentDeltaTime); void systemTaskSerial(void); void systemTaskBattery(void); +void systemTaskArduino(void); void systemTaskBaro(void); void systemTaskCompass(void); void systemTaskGps(void); diff --git a/UAV-ControlSystem/inc/config/eeprom.h b/UAV-ControlSystem/inc/config/eeprom.h index 1774a35..c1a66b0 100644 --- a/UAV-ControlSystem/inc/config/eeprom.h +++ b/UAV-ControlSystem/inc/config/eeprom.h @@ -159,6 +159,7 @@ typedef enum { EEPROM_PERIOD_RX_CLI, EEPROM_PERIOD_SERIAL, EEPROM_PERIOD_BATTERY, + EEPROM_PERIOD_ARDUINO, #ifdef BARO EEPROM_PERIOD_BARO, #endif diff --git a/UAV-ControlSystem/inc/system_variables.h b/UAV-ControlSystem/inc/system_variables.h index 0217fd9..b570055 100644 --- a/UAV-ControlSystem/inc/system_variables.h +++ b/UAV-ControlSystem/inc/system_variables.h @@ -14,7 +14,7 @@ #ifndef SYSTEM_VARIABLES_H_ #define SYSTEM_VARIABLES_H_ -#define EEPROM_SYS_VERSION 107 +#define EEPROM_SYS_VERSION 111 #define ADC_STATE #include "stm32f4xx.h" diff --git a/UAV-ControlSystem/src/Flight/pid.c b/UAV-ControlSystem/src/Flight/pid.c index c16c8ec..1d5d870 100644 --- a/UAV-ControlSystem/src/Flight/pid.c +++ b/UAV-ControlSystem/src/Flight/pid.c @@ -20,6 +20,10 @@ #include "drivers/failsafe_toggles.h" #include "drivers/motormix.h" #include "utilities.h" +#include "drivers/arduino_com.h" + +#define sq(x) ((x)*(x)) +#define map(x, in_min, in_max, out_min, out_max) (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min #define PTERM_SCALE 0.032029f /*P-term used as a scale value to the PID controller*/ @@ -63,7 +67,16 @@ pt1Filter_t accelFilter[2] = {0}; float accRollFineTune = 0; float accPitchFineTune = 0; - +float oldSensorValue[2] = {0}; +float oldSensorValueRoll[12] = {0}; +float oldSensorValuePitch[12] = {0}; +float MagnetFilteredOld[3]; +float alphaMagnet = 0.4; +int MagnetMax[3]; +int MagnetMin[3]; +float MagnetMap[3]; +float Yaw; +float YawU; /************************************************************************** * BRIEF: Calculates angle from accelerometer * @@ -124,9 +137,6 @@ float constrainf(float amt, int low, int high) } -float oldSensorValue[2] = {0}; -float oldSensorValueRoll[12] = {0}; -float oldSensorValuePitch[12] = {0}; int i = 0; @@ -214,6 +224,59 @@ void getCurrentValues(float sensorValues[3], uint8_t ID_profile) break; case PID_ID_COMPASS: + { + float xMagnetFiltered = 0; + float yMagnetFiltered = 0; + float zMagnetFiltered = 0; + xMagnetFiltered = MagnetFilteredOld[0] + alphaMagnet * (compass_data.x - MagnetFilteredOld[0]); + yMagnetFiltered = MagnetFilteredOld[1] + alphaMagnet * (compass_data.y - MagnetFilteredOld[1]); + zMagnetFiltered = MagnetFilteredOld[2] + alphaMagnet * (compass_data.z - MagnetFilteredOld[2]); + + MagnetFilteredOld[0] = xMagnetFiltered; + MagnetFilteredOld[1] = yMagnetFiltered; + MagnetFilteredOld[2] = zMagnetFiltered; + + + //this part is required to normalize the magnetic vector + if (xMagnetFiltered>MagnetMax[0]) { MagnetMax[0] = xMagnetFiltered; } + if (yMagnetFiltered>MagnetMax[1]) { MagnetMax[1] = yMagnetFiltered; } + if (zMagnetFiltered>MagnetMax[2]) { MagnetMax[2] = zMagnetFiltered; } + + if (xMagnetFiltered