HAPSRG / Mbed 2 deprecated HAPStail

Dependencies:   mbed MatrixMath LPS25HB_I2C LSM9DS1 Matrix2 PIDcontroller LoopTicker SBUS_without_mainfile UsaPack solaESKF_wind Vector3 CalibrateMagneto FastPWM

global.cpp

Committer:
osaka
Date:
2021-12-10
Revision:
85:0b14a2a600fc
Parent:
84:028bd650e8bc
Child:
87:981895e1d4f2

File content as of revision 85:0b14a2a600fc:

#include "global.hpp"

// var

// communication
Serial pc(USBTX, USBRX);
I2C i2c(PB_9,PB_8);  // sda, scl
UsaPack tail(PG_14, PG_9, 115200); // log - tail
SBUS sbus(PD_5, PD_6);

// sensor
LSM9DS1 lsm(i2c);
LPS lps(i2c);
//UsaPack sensor1(PC_10, PC_11, 115200);
//UsaPack sensor2(PC_12, PD_2, 115200);
CalibrateMagneto magCalibrator;

// io
DigitalIn userButton(USER_BUTTON);
DigitalIn locdef1(PG_2);
DigitalIn locdef2(PG_3);

// control
FastPWM servo(PE_9);
PID pitchPID(5.0, 0,0, PID_dt); // rad
PID pitchratePID(0.5, 0.0, 0.0, PID_dt); // rad/s
solaESKF eskf;
int itowVEL_log = 0;

int loop_count = 0;
float att_dt = 0.01f;
float rc[16];

int16_t ax, ay, az;
int16_t gx, gy, gz;
float magval[3] = {1.0f, 0.0f, 0.0f};

// elevator
float de;
float deobj;

// position
Matrix SensorAlignmentAG(3,3);
Matrix SensorAlignmentMAG(3,3);
Vector3 rpy(0.0f, 0.0f, 0.0f); // x:roll  y:pitch  z:yaw
Vector3 acc;
Vector3 accref(0.0f, 0.0f,  0.98f);
Vector3 mag;
Vector3 magref(0.65f, 0.0f, 0.75f);
Vector3 gyro;
Vector3 vb;
Matrix euler(3,1);
MedianFilter accMedian(3);
MedianFilter gyroMedian(3);
MedianFilter magMedian(3);
float palt;
float palt0;
bool headingUpdateFlag = false;
float dynaccnorm2;

int out1, out2;
float scaledServoOut[1] = {0.0f};
float servoOut[1] = {1500.0f};

float val_thmg = 0.0f;
float sigma_thmg = 0.0f;
float th_mg = 0.0f;
float accnormerr = 0.0f;
float sigma_accnorm = 0.0f;

int calibrationFlag = 0;
int pos_tail = 0; // 0:left 1:center 2:right
int agoffset[6] = {0, 0, 0, 0, 0, 0};
float magbiasMin[3] = {0.0f, 0.0f, 0.0f};
float magbiasMax[3] = {0.0f, 0.0f, 0.0f};
float accMin[3] = {-1.0f, -1.0f, -1.0f};
float accMax[3] = { 1.0f,  1.0f,  1.0f};

Vector3 rpy_align(0.0f*M_PI/180.0f, 0.0f*M_PI/180.0f, 0.0f);

// eepromのread writeのためのunionを定義
const int eeprom_address = 0xA0; // EEPROMの3つの入力が全て+より
const int eeprom_pointeraddress = 0;

// UsaPack
const int tail_address[3] = {1111, 2222, 3333};
const int time_address = 4444;
valuePack posValues;
updatePack updateValues;
Timer system_dt;

float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
    return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

void setDiag(Matrix& mat, float val){
    for (int i = 1; i < mat.getCols()+1; i++){
            mat(i,i) = val;
    }
}