Kristoffer Richardsson
Published © CC BY-SA

Bike turning signal

Be safe when biking at night with blinking lights when signalling to turn

IntermediateFull instructions provided2 hours2,152
Bike turning signal

Things used in this project

Story

Read more

Schematics

Mounting the LED-ring

Mount the LED-ring under the Crazyflie, as always
Pinheaders usage long 1 top one bottom

Code

Bike turn LED ring effect

C/C++
Add this snippet to deck/drivers/src/ledring12.c, for instance after the other effects.
/*************** Bike turn effect *******************/

static void bikeLightRender(uint8_t buffer[][3], int flashCount);
static void bikeLightFlash(uint8_t buffer[][3], bool on);

#define SET_ORANGE(dest, intensity) dest[0] = intensity; dest[1] = intensity / 2; dest[2] = 0;

#define BL_NORMAL 0
#define BL_TURNING 1

static int flashCount = 0;
#define NR_OF_FLASHES 4
#define COUNTS_PER_FLASH 20

static float gyroHist = 0.0;

static void bikeLight(uint8_t buffer[][3], bool reset) {
  static int gyroXId, gyroYId, gyroZId, accYId, accZId;
  static bool isInitialized = false;
  static int state = BL_NORMAL;

  if (!isInitialized) {
    gyroXId = logGetVarId("gyro", "x");
    gyroYId = logGetVarId("gyro", "y");
    gyroZId = logGetVarId("gyro", "z");
    accYId = logGetVarId("acc", "y");
    accZId = logGetVarId("acc", "z");
    isInitialized = true;
  }

  gyroHist = gyroHist * 0.8 + logGetFloat(gyroXId) + logGetFloat(gyroYId) + logGetFloat(gyroYId);
  float accY = logGetFloat(accYId);
  float accZ = logGetFloat(accZId);

  switch (state) {
    case BL_NORMAL:
      if (accY < -0.5 && accZ > -0.5 && gyroHist > 2500) {
        flashCount = NR_OF_FLASHES * COUNTS_PER_FLASH;
        state = BL_TURNING;
      }
      break;
    case BL_TURNING:
      flashCount--;
      if (flashCount <= 0) {
        state = BL_NORMAL;
      }
      bikeLightRender(buffer, flashCount);
      break;
    default:
      // Ignore
      break;
  }
}

static void bikeLightRender(uint8_t buffer[][3], int flashCount) {
  bool on = flashCount % COUNTS_PER_FLASH > COUNTS_PER_FLASH / 2;
  bikeLightFlash(buffer, on);
}

static void bikeLightFlash(uint8_t buffer[][3], bool on) {
  int intensity = 0;
  if (on) {
    intensity = 0xff;
  }

  int i;
  for (i = 0; i < NBR_LEDS; i++) {
  	SET_ORANGE(buffer[i], intensity);
  }
}

Register the LED ring effect and make it the default effect

C/C++
Register the new LED-ring effect. This code should not be pasted, but shows modifications needed. It also goes into deck/drivers/src/ledring12.c.
// Find this array and add the bikeLIght
Ledring12Effect effectsFct[] =
{
  blackEffect,
  whiteSpinEffect,
  colorSpinEffect,
  tiltEffect,
  brightnessEffect,
  spinEffect2,
  doubleSpinEffect,
  solidColorEffect,
  ledTestEffect,
  batteryChargeEffect,
  boatEffect,
  siren,
  gravityLight,
  virtualMemEffect,
  
  // Add here, index 14 in the array
  bikeLight,
}; //TODO Add more

// effect is holding the current effect.
// Find it and set it to our new bikeLight effect
static uint32_t effect = 14;

// If you want to log the gyroHist variable and plot it in the PC client, add this somewhere at the end
LOG_GROUP_START(ring2)
LOG_ADD(LOG_FLOAT, gyroHist, &gyroHist)
LOG_GROUP_STOP(ring2)

Credits

Kristoffer Richardsson

Kristoffer Richardsson

3 projects • 5 followers
Developer at Bitcraze

Comments