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:
NaotoMorita
Date:
2022-03-31
Revision:
120:9f4725deb5a6
Parent:
118:97ffe77b6f38
Child:
121:1d5b3e1f0d21

File content as of revision 120:9f4725deb5a6:

#include "global.hpp"

// var

bool hilFlag = false;

// communication
UsaPack pc(USBTX, USBRX, 115200);
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);
FastPWM rudServo(PE_11);
FastPWM motor1(PE_13);
FastPWM motor2(PA_6);
PID pitchPID(5.0, 0,0, PID_dt); // rad
PID pitchratePID(0.5, 0.0, 0.0, PID_dt); // rad/s
PID yawratePID(1.0, 0.0, 0.0, PID_dt); // rad/s
solaESKF eskf;
int itowVEL_log = 0;

Timer _t;
int loop_count = 0;
float att_dt = 0.01f;
float tstart = 0.0f;
float tgps = 0.0f;
float tsensors = 0.0f;
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;
float dr;
float drobj;
float dTl;
float dTr;

// 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 vi;
Vector3 pi;
Matrix euler(3,1);
float palt;
float palt0;
bool gpsUpdateFlag = false;
bool sensorUpdateFlag = false;
int sensorUpdateID = 1;
bool prefligt_finished = false;
float dynaccnorm2;
//float opt_vx;
//float opt_vy;
//const float theta_px = 42.0f * M_PI / 180.0f;
//const float theta_py = 42.0f * M_PI / 180.0f;
//const int pixel_x = 35;
//const int pixel_y = 35;

float scaledServoOut[2] = {0.0f,0.0f};
float servoOut[2] = {1500.0f,1500.0f};

float scaledMotorOut[2] = {-1.0f,-1.0f};
float motorOut[2] = {1000.0f,1000.0f};

int calibrationFlag = 0;
int pos_tail = 0; // 0:left 1:center 2:right
float agoffset[6] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
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};
int gpsitow = 0;

float roll_offset = 0.0f;
float pitch_offset = 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;
receivePack rp;
sendPack sp;
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;
    }
}

//alignment
int n_ave = 0;
float roll_sum = 0.0f;
float pitch_sum = 0.0f;