//=================================Include VESC========================================
#include <VescUart.h>
VescUart UART; //Initiate VescUart class
#include <movingAvg.h> // noch nicht verwendet
//=================================7Segment=====================================
#include <Arduino.h>
#include <AceTMI.h> // SimpleTmiInterface
#include <AceSegment.h> // Tm1637Module
using ace_tmi::SimpleTmiInterface;
using ace_segment::Tm1637Module;
const uint8_t CLK_PIN = 5;
const uint8_t DIO_PIN = 4;
const uint8_t NUM_DIGITS = 6;
const uint8_t DELAY_MICROS = 10;
using TmiInterface = SimpleTmiInterface;
TmiInterface tmiInterface(DIO_PIN, CLK_PIN, DELAY_MICROS);
Tm1637Module<TmiInterface, NUM_DIGITS> ledModule(tmiInterface);
void set(uint8_t* data, int number_of_bit, bool value) {
// setzt das Bit der Nummer "number_of_bit" in "data" auf "value"
uint8_t mask = 0b1 << number_of_bit;
if (value == 1) {
*data = *data | mask;
} else {
*data = *data & (~mask);
}
}
uint8_t convert_number(unsigned int number) {
uint8_t a;
switch (number) {
case 0:
a = 0b00111111;
break;
case 1:
a = 0b00000110;
break;
case 2:
a = 0b01011011;
break;
case 3:
a = 0b01001111;
break;
case 4:
a = 0b01100110;
break;
case 5:
a = 0b01101101;
break;
case 6:
a = 0b01111101;
break;
case 7:
a = 0b00000111;
break;
case 8:
a = 0b01111111;
break;
case 9:
a = 0b01101111;
break;
case 10: // A
a = 0b01111110;
break;
default:
a = 0b00110110;
}
return a;
}
//=================================ONE BUTTON=====================================
#include "OneButton.h"
const int BUTTON_PIN = 2; //INPUT
OneButton button = OneButton(
BUTTON_PIN, // Input pin for the button
true, // Button is active high
true // Disable internal pull-up resistor
);
//======================Pins======================================
//======================INPUTS======================================
//const int KnopfUeberwachenPIN = 2; //muss nicht da im one button initialisiert
const int Remote_PIN_A = 6;
const int Remote_PIN_B = 7;
const int Remote_PIN_C = 8;
const int Remote_PIN_D = 9;
//======================OUTPUTS======================================
const int LichtEinPIN = 3;
//======================Constants======================================
uint8_t white1, white2, red1, red2, icons4, icons5;
int digit_1, digit_2;
float voltage90 = 48; //5 bars
float voltage70 = 46.2; //4bars
float voltage50 = 44.16; //3bars
float voltage30 = 42; //2bars
float voltage10 = 39; //1bar
//red bar
int polepairs = 15; //(motor poles /2)
float motor_gear = 1; //set 1 for direct drive or hubs
float wheel_gear = 1; //set 1 for direct drive or hubs
int wheel_size = 25.1; //mm
float efficiency = 0.9; //(90% is pretty close in most cases)
float wheel_circumference = 0; //dont change it gets calculated
float gearing = 0; //dont change it gets calculated
//======================Variables=====================================================================
unsigned long current_time = 0;
int speeed = 0;
int voltage = 0;
int ERPM = 0;
float motor_rpm = 0;
unsigned long batteryupdate = 0;
unsigned long VescUpdate = 0;
bool Licht = 0; //1=Ein 0=Aus
bool Einschaltstatus = 1; //1=Ein 0=Aus
bool Mode1 = 0; //1=Ein 0=Aus
bool Mode2 = 0; //1=Ein 0=Aus
bool Mode3 = 1; //1=Ein 0=Aus
//==============================================================================================================================
//===============================SETUP==========================================================================================
//==============================================================================================================================
void setup() {
//======================IN/OUT PINS INIT======================================
//======================INPUTS======================================
//pinMode(KnopfUeberwachenPIN, INPUT); //muss nicht da im one button initialisiert
pinMode(Remote_PIN_A, INPUT);
pinMode(Remote_PIN_B, INPUT);
pinMode(Remote_PIN_C, INPUT);
pinMode(Remote_PIN_D, INPUT);
//======================OUTPUTS======================================
pinMode(LichtEinPIN, OUTPUT);
//=====================ONE BUTTON====================================
button.attachClick(singleclick); // link the function to be called on a singleclick event.
button.attachDoubleClick(doubleclick); // link the function to be called on a doubleclick event.
button.attachLongPressStart(longclick); // link the function to be called on a longpress event.
button.attachMultiClick(multiclick);
//======================UART / LED INIT======================================
tmiInterface.begin();
ledModule.begin();
// Serial.begin(500000); // Serial port for pc data (ALT)
// Serial1.begin(115200); // Serial port for vesc (ALT)
Serial.begin(115200); // Serial port for VESC
while (!Serial) {
;
}
UART.setSerialPort(&Serial); // Serial port for vesc
//================================Dauerhafte werte Berechnen===============================
wheel_circumference = wheel_size * 3.14159265359 / 1000; //m (wheel circumference in meters)
gearing = motor_gear / wheel_gear;
//================================LED TEST SIMULATION===============================
//LED test flash
ledModule.setPatternAt(0, 0b11111111);
ledModule.setPatternAt(2, 0b11111111);
ledModule.setPatternAt(4, 0b01111111);
ledModule.setPatternAt(5, 0b11111111);
ledModule.flush();
delay(500);
ledModule.setPatternAt(0, 0b00000000);
ledModule.setPatternAt(1, 0b00000000);
ledModule.setPatternAt(2, 0b00000000);
ledModule.setPatternAt(3, 0b00000000);
ledModule.setPatternAt(4, 0b00000000);
ledModule.setPatternAt(5, 0b00000000);
ledModule.flush();
set(&icons5, 0, 1); //set km/h
set(&icons5, 1, 0); //set mph
ledModule.setBrightness(7); // 0-7
set(&icons5, 4, 1); // Set Mode 3
ledModule.flush();
}
//==============================================================================================================================
//============================MainLoop==========================================================================================
//==============================================================================================================================
void loop() {
current_time = millis();
button.tick(); // check the status of the button
if (Einschaltstatus == 0) {// wenn ausgeschaltet
ledModule.setPatternAt(5, 0b10000000);
ledModule.flush();
delay(500);
}
else { // wenn eingeschaltet
set(&icons5, 7, 0);
//====================================== VESC UPDATE ========================================
if (current_time > VescUpdate + 200) {
VescUpdate = current_time;
//================================Vesc Daten Auslesen===============================
if ( UART.getVescValues() ) { // Call the function getVescValues() to acquire data from VESC
ERPM = (UART.data.rpm);
voltage = UART.data.inpVoltage;
//Serial.print(UART.data.inpVoltage);
//Serial.println(UART.data.ampHours);
//Serial.println(UART.data.tachometerAbs);
}
else
{
//Serial.println("Failed to get data!");
}
//motor_rpm = ERPM / polepairs;
//speeed = motor_rpm * gearing * wheel_circumference * 0.06; // Motor RPM (/min) * Gear Ratio() * Wheel Circumference (m) = m/Min m/min in kmh -> 0.06
speeed = ERPM / 348;
//Serial.print(" ");
//Serial.print(speeed);
//Serial.print(" ");
digit_2 = speeed % 10;
digit_1 = (speeed / 10) % 10;
//Serial.print(digit_1);
//Serial.print(" ");
//Serial.print(digit_2);
//Serial.println();
white2 = convert_number(digit_2);
if (speeed > 9)
{
white1 = convert_number(digit_1);
}
else {
white1 = 0b00000000;
}
ledModule.setPatternAt(0, white1);
ledModule.setPatternAt(2, white2);
ledModule.flush();
}
//====================================== BATTERY UPDATE ========================================
if (current_time > batteryupdate + 500) {
batteryupdate = current_time;
if (voltage >= voltage90) {
set(&icons4, 0, 1);
set(&icons4, 1, 1);
set(&icons4, 2, 1);
set(&icons4, 3, 1);
set(&icons4, 4, 1);
set(&icons4, 7, 0);
}
else if (voltage >= voltage70) {
set(&icons4, 0, 1);
set(&icons4, 1, 1);
set(&icons4, 2, 1);
set(&icons4, 3, 1);
set(&icons4, 4, 0);
set(&icons4, 7, 0);
}
else if (voltage >= voltage50) {
set(&icons4, 0, 1);
set(&icons4, 1, 1);
set(&icons4, 2, 1);
set(&icons4, 3, 0);
set(&icons4, 4, 0);
set(&icons4, 7, 0);
}
else if (voltage >= voltage30) {
set(&icons4, 0, 1);
set(&icons4, 1, 1);
set(&icons4, 2, 0);
set(&icons4, 3, 0);
set(&icons4, 4, 0);
set(&icons4, 7, 0);
}
else if (voltage >= voltage10) {
set(&icons4, 0, 1);
set(&icons4, 1, 0);
set(&icons4, 2, 0);
set(&icons4, 3, 0);
set(&icons4, 4, 0);
set(&icons4, 7, 0);
}
else {
set(&icons4, 0, 0);
set(&icons4, 1, 0);
set(&icons4, 2, 0);
set(&icons4, 3, 0);
set(&icons4, 4, 0);
set(&icons4, 7, 1);
}
ledModule.setPatternAt(4, icons4);
ledModule.setPatternAt(5, icons5);
ledModule.flush();
}
//delay(100);// =====================================DELAY MUSS WARSCHEINLICH NOCH WEG
}
}
//====================================== Licht Funktion ========================================
void singleclick() { // what happens when the button is clicked
if (Licht == 0) {
Licht = 1;
set(&icons5, 5, 1);
ledModule.flush();
}
else {
Licht = 0;
set(&icons5, 5, 0);
ledModule.flush();
}
digitalWrite(LichtEinPIN, Licht);
//Serial.print("Licht= ");
//Serial.println(Licht);
}
//====================================== Drive Mode (Macht noch nichts)========================================
void doubleclick() { // what happens when button is double-clicked
//Serial.println("Change Drive Mode");
if (Mode1 == 1) {
Mode1 = 0; set(&icons5, 2, 0);
Mode2 = 1; set(&icons5, 3, 1);
ledModule.flush();
}
else {
if (Mode2 == 1) {
Mode2 = 0; set(&icons5, 3, 0);
Mode3 = 1; set(&icons5, 4, 1);
ledModule.flush();
}
else {
if (Mode3 == 1) {
Mode3 = 0; set(&icons5, 4, 0);
Mode1 = 1; set(&icons5, 2, 1);
ledModule.flush();
}
else { }
}
}
}
//====================================== Ausschalten========================================
void longclick() { // what happens when buton is long-pressed
if (Einschaltstatus == 1) { // Wenn ein war dann Ausschelten
//LED test flash
ledModule.setPatternAt(0, 0b11111111);
ledModule.setPatternAt(2, 0b11111111);
ledModule.setPatternAt(4, 0b01111111);
ledModule.setPatternAt(5, 0b11111111);
ledModule.flush();
delay(2000);
ledModule.setPatternAt(0, 0b00000000);
ledModule.setPatternAt(1, 0b00000000);
ledModule.setPatternAt(2, 0b00000000);
ledModule.setPatternAt(3, 0b00000000);
ledModule.setPatternAt(4, 0b00000000);
ledModule.setPatternAt(5, 0b00000000);
ledModule.flush();
Einschaltstatus = 0;
}
else { // wenn ausgeschalten wieder einschalten
Einschaltstatus = 1;
}
}
//====================================== Unlock oder Unlock aus (macht noch nichts)========================================
void multiclick() { // what happens when buton is multi clicked
//Serial.print("Unlock_Toggle");
}