Mikey's Lab

An all out assault on ignorance

Arduino – iBus code

May
20

#include <string.h>

#define IBUS_BUFFSIZE 32 // Max iBus packet size (2 byte header, 14 channels x 2 bytes, 2 byte checksum)
#define IBUS_MAXCHANNELS 10
#define THROT_CHANNEL 2
#define RUDDER_CHANNEL 3
#define LEFT_PWM_PIN 5
#define RIGHT_PWM_PIN 6
#define LEFT_DIR_PIN 7
#define RIGHT_DIR_PIN 8

static uint8_t ibusIndex = 0;
static uint8_t ibus[IBUS_BUFFSIZE] = {0};
static uint16_t rcValue[IBUS_MAXCHANNELS];

int ThrotLeft = 0;
int ThrotRight = 0;
int DirLeft = 1;
int DirRight = 1;

 

void setup()
{
Serial.begin(115200);

pinMode(13, OUTPUT);
pinMode(LEFT_PWM_PIN, OUTPUT);
pinMode(RIGHT_PWM_PIN, OUTPUT);
pinMode(LEFT_DIR_PIN, OUTPUT);
pinMode(RIGHT_DIR_PIN, OUTPUT);

digitalWrite(13, HIGH); //Flash on rx update

Serial.println(“setup done!”);
}

void loop()
{
readRx();
UpdateOutput();
}

void UpdateOutput()
{
//now we need to do the fancy channel mixing shit
//first we assign the base throtle value to each channel
tmpLeft = rcValue[THROT_CHANNEL];
tmpRight = rcValue[THROT_CHANNEL];

ThrotLeft = (((tmpLeft + rcValue[RUDDER_CHANNEL] – 1000)/2000)*255);
ThrotRight = (((tmpRight + 2000 – rcValue[RUDDER_CHANNEL])/2000)*255);
}

void readRx()
{
uint8_t i;

if (Serial.available())
{
uint8_t val = Serial.read();
// Look for 0x2040 as start of packet
if (ibusIndex == 0 && val != 0x20) {
return;
}
if (ibusIndex == 1 && val != 0x40) {
ibusIndex = 0;
return;
}

if (ibusIndex < IBUS_BUFFSIZE) ibus[ibusIndex] = val;
ibusIndex++;

if (ibusIndex == IBUS_BUFFSIZE)
{
digitalWrite(13,LOW);
}

ibusIndex = 0;
rcValue[0] = (ibus[ 3] << 8) + ibus[ 2];
rcValue[1] = (ibus[ 5] << 8) + ibus[ 4];
rcValue[2] = (ibus[ 7] << 8) + ibus[ 6];
rcValue[3] = (ibus[ 9] << 8) + ibus[ 8];
rcValue[4] = (ibus[11] << 8) + ibus[10];
rcValue[5] = (ibus[13] << 8) + ibus[12];
rcValue[6] = (ibus[15] << 8) + ibus[14];
rcValue[7] = (ibus[17] << 8) + ibus[16];
rcValue[8] = (ibus[19] << 8) + ibus[18];
rcValue[9] = (ibus[21] << 8) + ibus[20];
digitalWrite(13,HIGH);
return;
}
}
}

Uncategorized Comments Off on Arduino – iBus code

Comments

Comments are closed.