Skip to content

Instantly share code, notes, and snippets.

@peterpoetzi
Created January 9, 2020 21:15
Show Gist options
  • Save peterpoetzi/1b63a4a844162196613871767189bd05 to your computer and use it in GitHub Desktop.
Save peterpoetzi/1b63a4a844162196613871767189bd05 to your computer and use it in GitHub Desktop.
#define debug 0
#define deadband 50
#define nchannels 4
typedef struct{
int16_t steer;
int16_t speed;
} Serialcommand;
volatile Serialcommand command;
void setup() {
for(int i=2;i<=5;i++)pinMode(i,INPUT);
if(!debug)Serial.begin(19200);
if(debug)Serial.begin(115200);
}
bool read_d(uint8_t i){
return PIND & (1 << i);
}
void loop() {
uint32_t timeout=0;
int channels[nchannels]={0,0,0,0};
for(uint8_t i=2;i<2+nchannels;i++){
while(read_d(i)!=0 && timeout<300000)timeout++;
while(read_d(i)!=1 && timeout<300000)timeout++;
long start=micros();
while(read_d(i)!=0 && timeout<300000)timeout++;
long end=micros();
channels[i-2]=end-start;
}
if(timeout>=300000){
for(uint8_t i=0;i<nchannels;i++)channels[i]=0;
}
for(uint8_t i=0;i<nchannels;i++){
if(debug)Serial.print(i);
if(debug)Serial.print(": ");
if(debug)Serial.println(channels[i]);
}
int mult=channels[2]-1100;
mult*=2.2;
if(mult>1000)mult=1000;
if(mult<0)mult=0;
int32_t speed=channels[1]-1500;
speed*=2.5;
if(speed>0 && speed<deadband)speed=0;else if(speed>0)speed-=deadband;
if(speed<0 && speed>-deadband)speed=0;else if(speed<0)speed+=deadband;
speed*=mult;
speed/=1000;
if(speed>1000)speed=1000;
if(speed<-1000)speed=-1000;
command.speed=speed;
int32_t steer=channels[0]-1500;
steer*=2.5;
if(steer>0 && steer<deadband)steer=0;else if(steer>0)steer-=deadband;
if(steer<0 && steer>-deadband)steer=0;else if(steer<0)steer+=deadband;
steer*=mult;
steer/=1000;
if(steer>1000)steer=1000;
if(steer<-1000)steer=-1000;
steer=-steer;
command.steer=steer;
if(channels[2]<1150 && channels[3]<1050 && channels[2]>1000 && channels[3]>1000){
//poweroff
command.speed=0;
command.steer=0;
//handle custom poweroff routine, i.e. by writing magic numbers to the boards (require firmware modification)
}
if(debug)Serial.println(timeout);
if(debug)Serial.println(command.speed);
if(debug)Serial.println(command.steer);
if(!debug)Serial.write((uint8_t *) &command.steer, sizeof(command.steer));
if(!debug)Serial.write((uint8_t *) &command.speed, sizeof(command.speed));
}
@ArtyTLT
Copy link

ArtyTLT commented Jun 1, 2022

contact me by mail [email protected] Thank you in advance.

@GhajariAli
Copy link

Hello Peter. Can you help me? My problem is that I can't properly program my Flipsky MX4 PPM remote control to the hoverboard board.

Hey, where you able to get it to work? I have the same issue with frsky

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment