|
|
|
|
@ -30,7 +30,11 @@
|
|
|
|
|
// // #define DEBUG_SERIAL_USART3
|
|
|
|
|
// *******************************************************************
|
|
|
|
|
|
|
|
|
|
#define VERSION_TAG "v test 0.1"
|
|
|
|
|
#define VERSION_TAG "V test 0.1"
|
|
|
|
|
#define BTN_PLUS 2
|
|
|
|
|
#define BTN_MOINS 3
|
|
|
|
|
#define BTN_MARCHE 4
|
|
|
|
|
#define MAX_SPEED 300
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ########################## DEFINES ##########################
|
|
|
|
|
@ -43,10 +47,11 @@
|
|
|
|
|
|
|
|
|
|
// ########################## macros ############################
|
|
|
|
|
|
|
|
|
|
#include <Wire.h>
|
|
|
|
|
#include <Adafruit_GFX.h>
|
|
|
|
|
#include <Adafruit_SSD1306.h>
|
|
|
|
|
#include <NintendoExtensionCtrl.h> // https://github.com/dmadison/NintendoExtensionCtrl
|
|
|
|
|
#include <EEPROM.h>
|
|
|
|
|
|
|
|
|
|
#include <FastLED.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// #include <SoftwareSerial.h>
|
|
|
|
|
@ -87,51 +92,44 @@ int cmd2;
|
|
|
|
|
|
|
|
|
|
int motor_test_direction = 1;
|
|
|
|
|
|
|
|
|
|
int accel_speed =10;
|
|
|
|
|
|
|
|
|
|
#define SCREEN_WIDTH 128 // OLED display width, in pixels
|
|
|
|
|
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
|
|
|
|
|
int eeprom_address = 0;
|
|
|
|
|
|
|
|
|
|
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
|
|
|
|
|
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
|
|
|
|
|
// Connect to a Nunchuk
|
|
|
|
|
Nunchuk nchuk;
|
|
|
|
|
int consigne_speed = 10;
|
|
|
|
|
int actual_speed = 0;
|
|
|
|
|
int btn_control = 20;
|
|
|
|
|
|
|
|
|
|
// ########################## SETUP ##########################
|
|
|
|
|
void setup()
|
|
|
|
|
{
|
|
|
|
|
bool run_mode, last_run, last_plus, last_moins, last_marche = false;
|
|
|
|
|
bool rel_marche = true;
|
|
|
|
|
|
|
|
|
|
nchuk.begin();
|
|
|
|
|
while (!nchuk.connect()) {
|
|
|
|
|
Serial.println("nunchuck not connected");
|
|
|
|
|
delay(1000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x64)
|
|
|
|
|
// Clear the buffer
|
|
|
|
|
display.clearDisplay();
|
|
|
|
|
display.display();
|
|
|
|
|
|
|
|
|
|
display.setTextSize(1); // Draw 1X-scale text
|
|
|
|
|
display.setTextColor(SSD1306_WHITE);
|
|
|
|
|
display.setCursor(int
|
|
|
|
|
((display.width() -
|
|
|
|
|
(strlen("Hoverboard Serial") * 6)) / 2), 0);
|
|
|
|
|
display.println("Hoverboard Serial");
|
|
|
|
|
display.setCursor(int
|
|
|
|
|
((display.width() -(strlen(VERSION_TAG) *6))/2), 8);
|
|
|
|
|
display.println(VERSION_TAG);
|
|
|
|
|
/*
|
|
|
|
|
display.print("Nunchuk ");
|
|
|
|
|
nchuk.connect()? display.print("") : display.print("not ");
|
|
|
|
|
display.println("connected");
|
|
|
|
|
|
|
|
|
|
display.drawCircle(80, 41, 22, SSD1306_WHITE);
|
|
|
|
|
*/
|
|
|
|
|
display.display();
|
|
|
|
|
// ################ Led definition ########################
|
|
|
|
|
|
|
|
|
|
#define LED_PIN 5
|
|
|
|
|
#define NUM_LEDS 1
|
|
|
|
|
#define BRIGHTNESS 64
|
|
|
|
|
#define LED_TYPE WS2812
|
|
|
|
|
#define COLOR_ORDER GRB
|
|
|
|
|
|
|
|
|
|
CRGB leds[NUM_LEDS];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ########################## SETUP ##########################
|
|
|
|
|
void setup()
|
|
|
|
|
{
|
|
|
|
|
pinMode(BTN_MARCHE, INPUT_PULLUP);
|
|
|
|
|
pinMode(BTN_PLUS, INPUT_PULLUP);
|
|
|
|
|
pinMode(BTN_MOINS, INPUT_PULLUP);
|
|
|
|
|
|
|
|
|
|
Serial1.begin(HOVER_SERIAL_BAUD); // RX, TX from arduino to TX RX on hoverboard board. ! be carreful 3v3
|
|
|
|
|
pinMode(LED_BUILTIN, OUTPUT);
|
|
|
|
|
|
|
|
|
|
consigne_speed = EEPROMReadInt(eeprom_address);
|
|
|
|
|
if (consigne_speed > MAX_SPEED) {
|
|
|
|
|
consigne_speed = MAX_SPEED ;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Serial.begin(SERIAL_BAUD);
|
|
|
|
|
if (Serial) {
|
|
|
|
|
Serial.println(VERSION_TAG);
|
|
|
|
|
@ -156,7 +154,6 @@ void Send(int16_t uSteer, int16_t uSpeed)
|
|
|
|
|
void Receive()
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
int old_cursorX, old_cursorY;
|
|
|
|
|
// Check for new data availability in the Serial buffer
|
|
|
|
|
if (Serial1.available()) {
|
|
|
|
|
incomingByte = Serial1.read(); // Read the incoming byte
|
|
|
|
|
@ -216,16 +213,6 @@ void Receive()
|
|
|
|
|
Serial.print(" 8: ");
|
|
|
|
|
Serial.println(Feedback.boardTemp);
|
|
|
|
|
}
|
|
|
|
|
display.setCursor(0, 30);
|
|
|
|
|
display.setTextSize(1); // Draw 1X-scale text
|
|
|
|
|
display.setTextColor(SSD1306_WHITE);
|
|
|
|
|
display.print("V : ");
|
|
|
|
|
old_cursorX = display.getCursorX();
|
|
|
|
|
old_cursorY = display.getCursorY();
|
|
|
|
|
display.fillRect(old_cursorX, old_cursorY, (6 * 5), 8, SSD1306_BLACK); // erase previous display
|
|
|
|
|
display.setCursor(old_cursorX, old_cursorY);
|
|
|
|
|
display.print(Feedback.batVoltage);
|
|
|
|
|
display.display();
|
|
|
|
|
} else {
|
|
|
|
|
if (Serial)
|
|
|
|
|
Serial.println("Non-valid data skipped");
|
|
|
|
|
@ -237,129 +224,96 @@ void Receive()
|
|
|
|
|
incomingBytePrev = incomingByte;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Nunchuk_control()
|
|
|
|
|
{
|
|
|
|
|
int old_cursorX, old_cursorY;
|
|
|
|
|
|
|
|
|
|
if (!nchuk.update()) {
|
|
|
|
|
nchuk.connect();
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cmd1 = map(nchuk.joyX(),0,256,150,-150); // x - axis. Nunchuck joystick readings range 30 - 230
|
|
|
|
|
cmd2 = map(nchuk.joyY(),0,256,-1000,1000); // y - axis
|
|
|
|
|
|
|
|
|
|
Serial.print("X : "); Serial.println(nchuk.joyX());
|
|
|
|
|
Serial.print("Y : "); Serial.println(nchuk.joyY());
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
display.setCursor(0, 40);
|
|
|
|
|
display.setTextSize(1); // Draw 1X-scale text
|
|
|
|
|
display.setTextColor(SSD1306_WHITE);
|
|
|
|
|
display.print("X : ");
|
|
|
|
|
old_cursorX = display.getCursorX();
|
|
|
|
|
old_cursorY = display.getCursorY();
|
|
|
|
|
display.fillRect(old_cursorX, old_cursorY, (6 * 5), 8, SSD1306_BLACK); // erase previous display
|
|
|
|
|
display.setCursor(old_cursorX, old_cursorY);
|
|
|
|
|
display.print(cmd1);
|
|
|
|
|
|
|
|
|
|
display.setCursor(0, 50);
|
|
|
|
|
display.setTextSize(1); // Draw 1X-scale text
|
|
|
|
|
display.setTextColor(SSD1306_WHITE);
|
|
|
|
|
display.print("y : ");
|
|
|
|
|
old_cursorX = display.getCursorX();
|
|
|
|
|
old_cursorY = display.getCursorY();
|
|
|
|
|
display.fillRect(old_cursorX, old_cursorY, (6 * 5), 8, SSD1306_BLACK); // erase previous display
|
|
|
|
|
display.setCursor(old_cursorX, old_cursorY);
|
|
|
|
|
display.print(cmd2);
|
|
|
|
|
|
|
|
|
|
display.display();
|
|
|
|
|
|
|
|
|
|
// Send(cmd1, cmd2);
|
|
|
|
|
Send(0, cmd2);
|
|
|
|
|
}
|
|
|
|
|
// ########################## nunchuk ##########################
|
|
|
|
|
|
|
|
|
|
void Nunchuk_display()
|
|
|
|
|
{
|
|
|
|
|
if (!nchuk.update()) {
|
|
|
|
|
if (Serial) {
|
|
|
|
|
Serial.println("Controller disconnected!");
|
|
|
|
|
}
|
|
|
|
|
nchuk.connect();
|
|
|
|
|
// #############################
|
|
|
|
|
//This function will write a 2 byte integer to the eeprom at the specified address and address + 1
|
|
|
|
|
void EEPROMWriteInt(int p_address, int p_value)
|
|
|
|
|
{
|
|
|
|
|
byte lowByte = ((p_value >> 0) & 0xFF);
|
|
|
|
|
byte highByte = ((p_value >> 8) & 0xFF);
|
|
|
|
|
|
|
|
|
|
EEPROM.write(p_address, lowByte);
|
|
|
|
|
EEPROM.write(p_address + 1, highByte);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//This function will read a 2 byte integer from the eeprom at the specified address and address + 1
|
|
|
|
|
unsigned int EEPROMReadInt(int p_address)
|
|
|
|
|
{
|
|
|
|
|
byte lowByte = EEPROM.read(p_address);
|
|
|
|
|
byte highByte = EEPROM.read(p_address + 1);
|
|
|
|
|
|
|
|
|
|
return ((lowByte << 0) & 0xFF) + ((highByte << 8) & 0xFF00);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void run_control(void) {
|
|
|
|
|
if (!digitalRead(BTN_MARCHE)) {
|
|
|
|
|
if (last_marche){
|
|
|
|
|
if (!run_mode ){
|
|
|
|
|
if (rel_marche){
|
|
|
|
|
// run
|
|
|
|
|
run_mode = true;
|
|
|
|
|
rel_marche = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (rel_marche){
|
|
|
|
|
rel_marche = false;
|
|
|
|
|
run_mode = false;
|
|
|
|
|
actual_speed = 0;
|
|
|
|
|
if ( EEPROMReadInt(eeprom_address) != consigne_speed ){
|
|
|
|
|
EEPROMWriteInt(eeprom_address, consigne_speed);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
last_marche = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (Serial) {
|
|
|
|
|
// Read a button (on/off, C and Z)
|
|
|
|
|
Serial.print("Z: ");
|
|
|
|
|
nchuk.buttonZ()? Serial.print("On ") : Serial.print("Off ");
|
|
|
|
|
Serial.print("C: ");
|
|
|
|
|
nchuk.buttonC()? Serial.print("On ") : Serial.print("Off ");
|
|
|
|
|
|
|
|
|
|
// Read joystick axis (0-255, X and Y)
|
|
|
|
|
Serial.print("The joystick's Y axis is at ");
|
|
|
|
|
Serial.print(nchuk.joyY());
|
|
|
|
|
Serial.print(" and X axis is at ");
|
|
|
|
|
Serial.print(nchuk.joyX());
|
|
|
|
|
|
|
|
|
|
// Read an accelerometer and print values (0-1023, X, Y, and Z)
|
|
|
|
|
Serial.print(" - The accelerometer's X-axis is at ");
|
|
|
|
|
Serial.println(nchuk.accelX());
|
|
|
|
|
}
|
|
|
|
|
rel_marche = true;
|
|
|
|
|
last_marche = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ########################## nunchuk ##########################
|
|
|
|
|
|
|
|
|
|
void show_dot()
|
|
|
|
|
{
|
|
|
|
|
int old_cursorX, old_cursorY;
|
|
|
|
|
|
|
|
|
|
char text[22];
|
|
|
|
|
|
|
|
|
|
if (nchuk.update()) {
|
|
|
|
|
display.setCursor(0, 20);
|
|
|
|
|
display.setTextSize(1); // Draw 1X-scale text
|
|
|
|
|
display.setTextColor(SSD1306_WHITE);
|
|
|
|
|
display.print("X : ");
|
|
|
|
|
old_cursorX = display.getCursorX();
|
|
|
|
|
old_cursorY = display.getCursorY();
|
|
|
|
|
display.fillRect(old_cursorX, old_cursorY, (6 * 4), 8, SSD1306_BLACK); // erase previous display
|
|
|
|
|
display.setCursor(old_cursorX, old_cursorY);
|
|
|
|
|
display.print(nchuk.joyX());
|
|
|
|
|
display.setCursor(0, 50);
|
|
|
|
|
display.setTextSize(1); // Draw 1X-scale text
|
|
|
|
|
display.setTextColor(SSD1306_WHITE);
|
|
|
|
|
display.print("Y : ");
|
|
|
|
|
old_cursorX = display.getCursorX();
|
|
|
|
|
old_cursorY = display.getCursorY();
|
|
|
|
|
display.fillRect(old_cursorX, old_cursorY, (6 * 4), 8, SSD1306_BLACK); // erase previous display
|
|
|
|
|
display.setCursor(old_cursorX, old_cursorY);
|
|
|
|
|
display.print(nchuk.joyY());
|
|
|
|
|
|
|
|
|
|
int y_d = int(41 + (44*(nchuk.joyY()-128))/256);
|
|
|
|
|
int x_d = int(80 + (44*(nchuk.joyX()-128))/256);
|
|
|
|
|
|
|
|
|
|
if (Serial) {
|
|
|
|
|
sprintf (text,"y_d : %d | x_d : %d", y_d , x_d );
|
|
|
|
|
Serial.println(text);
|
|
|
|
|
|
|
|
|
|
void speed_control(void) {
|
|
|
|
|
if (run_mode){
|
|
|
|
|
if (!digitalRead(BTN_PLUS)){
|
|
|
|
|
if (last_plus){
|
|
|
|
|
consigne_speed = consigne_speed + 5;
|
|
|
|
|
if (consigne_speed > MAX_SPEED) {
|
|
|
|
|
consigne_speed = MAX_SPEED ;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
last_plus = true;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
last_plus = false;
|
|
|
|
|
}
|
|
|
|
|
if (!digitalRead(BTN_MOINS)){
|
|
|
|
|
if (last_moins){
|
|
|
|
|
consigne_speed = consigne_speed - 5;
|
|
|
|
|
if (consigne_speed < 0 ) {
|
|
|
|
|
consigne_speed = 0 ;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
last_moins = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
display.drawPixel(x_d,y_d,SSD1306_WHITE);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
nchuk.connect();
|
|
|
|
|
} else {
|
|
|
|
|
last_moins = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ########################## LOOP ##########################
|
|
|
|
|
|
|
|
|
|
unsigned long iTimeSend = 0;
|
|
|
|
|
int iTestMax = SPEED_MAX_TEST;
|
|
|
|
|
int iTest = 0;
|
|
|
|
|
int16_t old_cursorX;
|
|
|
|
|
int16_t old_cursorY;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void loop(void)
|
|
|
|
|
{
|
|
|
|
|
@ -368,24 +322,47 @@ void loop(void)
|
|
|
|
|
// Check for new received data
|
|
|
|
|
Receive();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (iTimeSend > timeNow)
|
|
|
|
|
return;
|
|
|
|
|
iTimeSend = timeNow + TIME_SEND;
|
|
|
|
|
// Nunchuk_display();
|
|
|
|
|
Nunchuk_control();
|
|
|
|
|
/*
|
|
|
|
|
// Send commands
|
|
|
|
|
if (iTimeSend > timeNow)
|
|
|
|
|
return;
|
|
|
|
|
iTimeSend = timeNow + TIME_SEND;
|
|
|
|
|
Send(0, cmd2);
|
|
|
|
|
if (Serial) {
|
|
|
|
|
if (run_mode != last_run) {
|
|
|
|
|
last_run = run_mode;
|
|
|
|
|
if (run_mode){
|
|
|
|
|
Serial.println("Run");
|
|
|
|
|
} else {
|
|
|
|
|
Serial.println("Stop");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (run_mode){
|
|
|
|
|
if ( actual_speed != consigne_speed){
|
|
|
|
|
if ( actual_speed < consigne_speed){
|
|
|
|
|
actual_speed ++ ;
|
|
|
|
|
} else {
|
|
|
|
|
actual_speed --;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
Send(0,actual_speed);
|
|
|
|
|
if ( actual_speed != accel_speed){
|
|
|
|
|
accel_speed = actual_speed;
|
|
|
|
|
if (Serial) Serial.println(actual_speed);
|
|
|
|
|
|
|
|
|
|
// Calculate test command signal
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
run_control();
|
|
|
|
|
btn_control --;
|
|
|
|
|
if (btn_control == 0){
|
|
|
|
|
btn_control =10;
|
|
|
|
|
speed_control();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (motor_test_direction == 1) cmd2 += 1;
|
|
|
|
|
else cmd2 -= 1;
|
|
|
|
|
if (abs(cmd2) > SPEED_MAX_TEST) motor_test_direction = -motor_test_direction;
|
|
|
|
|
*/
|
|
|
|
|
// Nunchuk_control();
|
|
|
|
|
// Blink the LED
|
|
|
|
|
digitalWrite(LED_BUILTIN, (timeNow % 2000) < 1000);
|
|
|
|
|
}
|
|
|
|
|
|