Baseflight

Post Reply
Katch
Posts: 280
Joined: Thu Aug 04, 2011 1:44 pm

Baseflight

Post by Katch »

Just wondering if anyone could list all the sensors currently supported by baseflight?

rotary65
Posts: 30
Joined: Mon Jun 25, 2012 11:27 am

Re: Baseflight

Post by rotary65 »

Based on the current source:

ADXL345 (Accelerator)
BMP085 (barometer)
hcsr04 (sonar)
hmc5883 (magnetic compass)
i2c/ledring (led ring)
mma845x (acc)
mpu3050 (gyro)
mpu6050 (gyro & acc)

dr.tom
Posts: 141
Joined: Fri Mar 30, 2012 4:46 pm
Location: Croatia
Contact:

Re: Baseflight - MS5611

Post by dr.tom »

from: viewtopic.php?f=8&t=1982&p=18757#p18715
marbalon wrote:Yesterday I code new algorithm for Alt holt + ACC Z. Idea is simple - It it still standard PID for baro but EstAltit and speed for D is filtered using ACC Z readings. I will post my algorithm in the evening, but it is coded on custom stm32 board but should works on Atmega too.
There is only one thing - MS5611 have better precision on when your read it via SPI.
In datasheet you can fund information that data line should not bee touched during ADC conversion, but maybe it is important for i2c (missing CS). Here is video.

http://www.youtube.com/watch?v=tEIf7yYl-Pk


Wow, super! :) So MS5611 will be supported soon

Is it possible that you upload driver files needed to make MS5611 work with baseflight firmware?
I have this board and flashed it with latest version
but baro is still not supported. http://www.goodluckbuy.com/kcopter-stm3 ... -chip.html

Thank you very much for your effort! :)

alexia
Posts: 85
Joined: Sun Jun 17, 2012 10:23 pm
Contact:

Re: Baseflight

Post by alexia »

wooo great working

marbalon
Posts: 107
Joined: Thu Aug 18, 2011 10:59 am

Re: Baseflight

Post by marbalon »

Here is MS5611 SPI driver if someone looking for.

Code: Select all

#include "board.h"
#include "mw.h"

// ************************************************************************************************************
// I2C/SPI Barometer MS561101BA
// ************************************************************************************************************
// first contribution from Fabio
// modification from Alex (September 2011)
//
// specs are here: http://www.meas-spec.com/downloads/MS5611-01BA03.pdf

// registers of the device
#define MS561101BA_PRESSURE    0x40
#define MS561101BA_TEMPERATURE 0x50
#define MS561101BA_RESET       0x1E

// OSR (Over Sampling Ratio) constants
#define MS561101BA_OSR_256  0x00
#define MS561101BA_OSR_512  0x02
#define MS561101BA_OSR_1024 0x04
#define MS561101BA_OSR_2048 0x06
#define MS561101BA_OSR_4096 0x08

#define OSR MS561101BA_OSR_4096
static int32_t  pressure;

static struct {
  // sensor registers from the MS561101BA datasheet
  uint16_t c[8];
  union {uint32_t val; uint8_t raw[4]; } ut; //uncompensated T
  union {uint32_t val; uint8_t raw[4]; } up; //uncompensated P
  uint8_t  state;
  uint32_t deadline;
} ms561101ba_ctx;

uint8_t baro_read_reg(uint8_t reg)
{
   uint8_t byte;
   ENABLE_BARO;
   spi_rw(reg);
   byte = spi_rw(0x00);
   DISABLE_BARO;

   return byte;
}

void baro_write_reg(uint8_t reg, uint8_t val)
{
   ENABLE_BARO;
   spi_rw(reg);
   spi_rw(val);
   DISABLE_BARO;
}


void MS561101BA_reset(){
  ENABLE_BARO;
  spi_rw(MS561101BA_RESET);
  delay(3);
  DISABLE_BARO;
}

static bool MS561101BA_CRC(uint16_t* prom) {
   int32_t i, j;
   uint32_t res = 0;
   uint8_t crc = prom[7] & 0xF;
   prom[7] &= 0xFF00;
   for (i = 0; i < 16; i++) {
     if (i & 1) res ^= ((prom[i>>1]) & 0x00FF);
     else res ^= (prom[i>>1]>>8);
     for (j = 8; j > 0; j--) {
       if (res & 0x8000) res ^= 0x1800;
       res <<= 1;
     }
   }
   prom[7] |= crc;
   if (crc == ((res >> 12) & 0xF)) return true;
   else return false;
}

void MS561101BA_readCalibration(){
  union {uint16_t val; uint8_t raw[2]; } data;
  uint8_t i;

//read first prom settings
  for(i=0;i<8;i++) {
    ENABLE_BARO;
    spi_rw(0xA0+2*i);
    data.raw[1] = spi_rw(0x0);  // read a 16 bit register
    data.raw[0] = spi_rw(0x0);
    ms561101ba_ctx.c[i] = data.val;
   DISABLE_BARO;
   delay(1);
  }
  DISABLE_BARO;
}

bool Baro_init() {
  delay(10);
  MS561101BA_reset();
  MS561101BA_readCalibration();
  return MS561101BA_CRC(&ms561101ba_ctx.c[0]);
}

// read uncompensated temperature value: send command first
void MS561101BA_UT_Start() {
  ENABLE_BARO;
  spi_rw(MS561101BA_TEMPERATURE + OSR);
  DISABLE_BARO;
}

// read uncompensated pressure value: send command first
void MS561101BA_UP_Start () {
  ENABLE_BARO;
  spi_rw(MS561101BA_PRESSURE + OSR);
  DISABLE_BARO;
}

// read uncompensated pressure value: read result bytes
void MS561101BA_UP_Read () {
  ENABLE_BARO;
  spi_rw(0x0);
  ms561101ba_ctx.up.raw[2] = spi_rw(0x0);
  ms561101ba_ctx.up.raw[1] = spi_rw(0x0);
  ms561101ba_ctx.up.raw[0] = spi_rw(0x0);
  DISABLE_BARO;
}

// read uncompensated temperature value: read result bytes
void MS561101BA_UT_Read() {
  ENABLE_BARO;
  spi_rw(0x0);
  ms561101ba_ctx.ut.raw[2] = spi_rw(0x0);
  ms561101ba_ctx.ut.raw[1] = spi_rw(0x0);
  ms561101ba_ctx.ut.raw[0] = spi_rw(0x0);
  DISABLE_BARO;

}

void MS561101BA_Calculate() {
  int32_t temperature,off2=0,sens2=0,delt;

  int32_t dT   = ms561101ba_ctx.ut.val - ((uint32_t)ms561101ba_ctx.c[5] << 8);
  int64_t off  = ((uint32_t)ms561101ba_ctx.c[2] <<16) + (((int64_t)dT * ms561101ba_ctx.c[4]) >> 7);
  int64_t sens = ((uint32_t)ms561101ba_ctx.c[1] <<15) + (((int64_t)dT * ms561101ba_ctx.c[3]) >> 8);
  temperature  = 2000 + (((int64_t)dT * ms561101ba_ctx.c[6])>>23);

  if (temperature < 2000) { // temperature lower than 20st.C
    delt = temperature-2000;
    delt  = delt*delt;
    off2  = (5 * delt)>>1;
    sens2 = (5 * delt)>>2;
    if (temperature < -1500) { // temperature lower than -15st.C
      delt  = temperature+1500;
      delt  = delt*delt;
      off2  += 7 * delt;
      sens2 += (11 * delt)>>1;
    }
  }
  off  -= off2;
  sens -= sens2;
  pressure     = (( (ms561101ba_ctx.up.val * sens ) >> 21) - off) >> 15;
}

void Baro_update() {
  if (micros() < ms561101ba_ctx.deadline) return;
  ms561101ba_ctx.deadline = micros();
  switch (ms561101ba_ctx.state) {
    case 0:
      MS561101BA_UT_Start();
      ms561101ba_ctx.state++; ms561101ba_ctx.deadline += 10000; //according to the specs, the pause should be at least 8.22ms
      break;
    case 1:
      MS561101BA_UT_Read();
      ms561101ba_ctx.state++;
      break;
    case 2:
      MS561101BA_UP_Start();
      ms561101ba_ctx.state++; ms561101ba_ctx.deadline += 10000; //according to the specs, the pause should be at least 8.22ms
      break;
    case 3:
      MS561101BA_UP_Read();
      MS561101BA_Calculate();
      BaroAlt = (1.0f - pow((float)pressure/101325.0f, 0.190295f)) * 4433000.0f; //centimeter
    
     if (LoggerIdx!= -1 && LoggerIdx < LOGGER_LEN)
      LoggerTab[LoggerIdx++] = BaroAlt;

      ms561101ba_ctx.state = 0; ms561101ba_ctx.deadline += 4000;
      break;
  }
}

Katch
Posts: 280
Joined: Thu Aug 04, 2011 1:44 pm

Re: Baseflight

Post by Katch »

Nice - now we need the Aux I2C working on the MPU6050 so that the mag will work on the FreeIMU 0.4.3 and its clones.

Post Reply