Skip to content

Instantly share code, notes, and snippets.

@iscle
Created May 16, 2018 18:25
Show Gist options
  • Save iscle/ba6b96ae57336c7717b587f61384a9c2 to your computer and use it in GitHub Desktop.
Save iscle/ba6b96ae57336c7717b587f61384a9c2 to your computer and use it in GitHub Desktop.
#include <RF24.h>
#include <SPI.h>
#include <VESC.h>
//#define DEBUG
typedef struct {
uint8_t x;
uint8_t y;
bool c;
bool z;
uint8_t vBat;
} RemoteData;
RemoteData remotedata;
mc_values values;
chuck_data chuckdata;
const byte addresses[][6] = {"PB001","PR001"};
unsigned long now, lastTransmission = 0;
RF24 radio(PA3, PA4);
void bldc_val_received(mc_values *val) {
//main_printf("Duty cycle: %.1f %%\r\n", val->duty_now * 100.0);
//main_printf("Fault Code: %s\r\n", bldc_interface_fault_to_string(val->fault_code));
}
void vescSetup() {
VESC_init();
VESC_setRxValueFunc(bldc_val_received);
chuckdata.js_x = 127;
chuckdata.js_y = 127;
chuckdata.bt_c = false;
chuckdata.bt_z = false;
chuckdata.acc_x = 0;
chuckdata.acc_y = 0;
chuckdata.acc_z = 0;
}
void setup() {
#ifdef DEBUG
// Serial Setup (Debug only)
Serial.begin(115200);
#endif
// Radio Setup
radio.begin();
radio.setPALevel(RF24_PA_MAX);
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1,addresses[0]);
radio.startListening();
// Initialise Data
remotedata.x = 127;
remotedata.y = 127;
remotedata.c = false;
remotedata.z = false;
remotedata.vBat = 0.0;
vescSetup();
}
void loop() {
now = millis();
if (radio.available()){
radio.read(&remotedata, sizeof(RemoteData));
lastTransmission = now;
}
if (now - lastTransmission > 1000) {
remotedata.x = 127;
remotedata.y = 128;
remotedata.c = false;
remotedata.z = false;
remotedata.vBat = 0.0;
}
chuckdata.js_x = remotedata.x;
chuckdata.js_y = remotedata.y;
chuckdata.bt_c - remotedata.c;
chuckdata.bt_z = remotedata.z;
VESC_setChuckData(&chuckdata);
//VESC_getValues();
#ifdef DEBUG
Serial.print("x: ");
Serial.print(remotedata.x);
Serial.print(" y: ");
Serial.print(remotedata.y);
Serial.print(" c: ");
Serial.print(remotedata.c);
Serial.print(" z: ");
Serial.print(remotedata.z);
Serial.print(" vBat: ");
Serial.println(remotedata.vBat*5.0/255.0);
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment