diff --git a/src/AppConfiguration.cpp b/src/AppConfiguration.cpp index bd2fffe..45ca51a 100644 --- a/src/AppConfiguration.cpp +++ b/src/AppConfiguration.cpp @@ -44,8 +44,9 @@ void AppConfiguration::readPreferences() { config.lightFadingDuration = doc["lightFadingDuration"] | 220; config.lightMaxBrightness = doc["lightMaxBrightness"] | MAX_BRIGHTNESS; config.brakeLightEnabled = doc["brakeLightEnabled"] | true; - config.authToken = doc["authToken"] | BLYNK_AUTH_TOKEN; +#if defined(CANBUS_ENABLED) config.vescId = doc["vescId"] | VESC_CAN_ID; +#endif config.numberPixelLight = doc["numberPixelLight"] | NUMPIXELS; config.numberPixelBatMon = doc["numberPixelBatMon"] | LIGHT_BAR_NUMPIXELS; // calculate RGB values for primary and secondary color @@ -88,7 +89,6 @@ void AppConfiguration::savePreferences() { doc["lightFadingDuration"] = config.lightFadingDuration; doc["lightMaxBrightness"] = config.lightMaxBrightness; doc["brakeLightEnabled"] = config.brakeLightEnabled; - doc["authToken"] = config.authToken; doc["vescId"] = config.vescId; doc["numberPixelLight"] = config.numberPixelLight; doc["numberPixelBatMon"] = config.numberPixelBatMon; diff --git a/src/BatteryMonitor.cpp b/src/BatteryMonitor.cpp index 603a4f6..ec41845 100644 --- a/src/BatteryMonitor.cpp +++ b/src/BatteryMonitor.cpp @@ -15,12 +15,9 @@ int batteryReadings[numBatReadings]; // the batteryReadings from the analog in double currentReadings[numCurReadings]; -BatteryMonitor::BatteryMonitor() {} -#ifdef CANBUS_ENABLED -BatteryMonitor::BatteryMonitor(CanBus::VescData *vescData) { +BatteryMonitor::BatteryMonitor(VescData *vescData) { this->vescData = vescData; } -#endif void BatteryMonitor::init() { @@ -44,7 +41,7 @@ float BatteryMonitor::readValues() { int adc = smoothAnalogReading(); // read the sensor and smooth the value float sensorValue = ( adc * 3.3 ) / (4096); // calculate the voltage at the ESP32 GPIO float voltage = sensorValue * VOLTAGE_DIVIDER_CONSTANT; // calculate the battery voltage - voltage = vescData->inputVoltage + AppConfiguration::getInstance()->config.batteryDrift; + voltage = voltage+ AppConfiguration::getInstance()->config.batteryDrift; #else float voltage = vescData->inputVoltage; float current = abs(vescData->current); diff --git a/src/BatteryMonitor.h b/src/BatteryMonitor.h index 930e7d3..09cc589 100644 --- a/src/BatteryMonitor.h +++ b/src/BatteryMonitor.h @@ -13,10 +13,7 @@ class BatteryMonitor { public: - BatteryMonitor(); -#ifdef CANBUS_ENABLED - BatteryMonitor(CanBus::VescData *vescData); -#endif + BatteryMonitor(VescData *vescData); void init(); float readValues(); void checkValues(); @@ -26,9 +23,7 @@ class BatteryMonitor { int max_voltage = 0; int warn_voltage = 0; double max_current = 0; -#ifdef CANBUS_ENABLED - CanBus::VescData *vescData; -#endif + VescData *vescData; int smoothAnalogReading(); void updateCurrentArray(double value); double getAverageCurrent(); diff --git a/src/BleCanProxy.cpp b/src/BleCanProxy.cpp index fcf1e94..e8b954d 100644 --- a/src/BleCanProxy.cpp +++ b/src/BleCanProxy.cpp @@ -1,5 +1,6 @@ #include "BleCanProxy.h" +#if defined(CANBUS_ENABLED) BleCanProxy::BleCanProxy(CanDevice *candevice, Stream *stream, uint8_t vesc_id, uint8_t ble_proxy_can_id) { this->candevice = candevice; this->stream = stream; @@ -179,3 +180,4 @@ void BleCanProxy::proxyOut(uint8_t *data, unsigned int size, uint8_t crc1, uint8 //Serial.println(""); } +#endif \ No newline at end of file diff --git a/src/BleServer.cpp b/src/BleServer.cpp index cc17ad2..6625038 100644 --- a/src/BleServer.cpp +++ b/src/BleServer.cpp @@ -136,7 +136,11 @@ void BleServer::onMTUChange(uint16_t MTU, ble_gap_conn_desc* desc) { BLE_PACKET_SIZE = MTU_SIZE - 3; } +#if defined(CANBUS_ENABLED) void BleServer::init(Stream *vesc, CanBus *canbus) { +#else +void BleServer::init(Stream *vesc) { +#endif vescSerial = vesc; // Create the BLE Device @@ -146,7 +150,9 @@ void BleServer::init(Stream *vesc, CanBus *canbus) { char buf[128]; snprintf(buf, 128, "Initial MTU size %d", mtu_size); Logger::notice(LOG_TAG_BLESERVER, buf); +#if defined(CANBUD_ENABLED) this->canbus = canbus; +#endif // Create the BLE Server pServer = NimBLEDevice::createServer(); @@ -224,7 +230,7 @@ void BleServer::init(Stream *vesc, CanBus *canbus) { #ifdef CANBUS_ENABLED -void BleServer::loop(CanBus::VescData *vescData, long loopTime, long maxLoopTime) { +void BleServer::loop(VescData *vescData, long loopTime, long maxLoopTime) { #else void BleServer::loop() { #endif @@ -464,6 +470,8 @@ void BleServer::updateRescueApp(long loopTime, long maxLoopTime) { */ } +#endif //CANBUS_ENABLED + template void BleServer::sendValue(std::string key, TYPE value) { std::stringstream ss; @@ -510,5 +518,3 @@ struct BleServer::sendConfigValue { void BleServer::sendConfig() { visit_struct::for_each(AppConfiguration::getInstance()->config, sendConfigValue(pCharacteristicConf)); } - -#endif //CANBUS_ENABLED \ No newline at end of file diff --git a/src/BleServer.h b/src/BleServer.h index 1797c09..b1d92b1 100644 --- a/src/BleServer.h +++ b/src/BleServer.h @@ -11,7 +11,7 @@ #define LOG_TAG_BLESERVER "BleServer" -#define VESC_SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" +#define VESC_SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" #define VESC_CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E" #define VESC_CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E" @@ -22,15 +22,16 @@ #define RESCUE_CHARACTERISTIC_UUID_HW_VERSION "99EB1515-A9E9-4024-B0A4-3DC4B4FABFB0" class BleServer : - public NimBLEServerCallbacks, + public NimBLEServerCallbacks, public BLECharacteristicCallbacks { public: BleServer(); - void init(Stream *vesc, CanBus *canbus); #ifdef CANBUS_ENABLED - void loop(CanBus::VescData *vescData, long loopTime, long maxLoopTime); + void init(Stream *vesc, CanBus *canbus); + void loop(VescData *vescData, long loopTime, long maxLoopTime); #else - void loop(); + void init(Stream *vesc); + void loop(); #endif // NimBLEServerCallbacks @@ -43,15 +44,17 @@ class BleServer : void onSubscribe(NimBLECharacteristic* pCharacteristic, ble_gap_conn_desc* desc, uint16_t subValue); void onStatus(NimBLECharacteristic* pCharacteristic, Status status, int code); void sendConfig(); + template + void sendValue(std::string key, TYPE value); -#ifdef CANBUS_ENABLED +#if defined(CANBUS_ENABLED) void updateRescueApp(long loopTime, long maxLoopTime); - template - void sendValue(std::string key, TYPE value); #endif private: +#if defined(CANBUS_ENABLED) CanBus *canbus{}; +#endif struct sendConfigValue; void dumpBuffer(std::string header, std::string buffer); }; diff --git a/src/CanBus.cpp b/src/CanBus.cpp index 231db37..8cc41a5 100644 --- a/src/CanBus.cpp +++ b/src/CanBus.cpp @@ -5,8 +5,9 @@ #ifdef CANBUS_ENABLED -CanBus::CanBus() { - stream = new LoopbackStream(BUFFER_SIZE); +CanBus::CanBus(VescData *vescData) { + this->vescData = vescData; + this->stream = new LoopbackStream(BUFFER_SIZE); } void CanBus::init() { @@ -176,32 +177,32 @@ void CanBus::processFrame(CAN_frame_t rx_frame, int frameCount) { uint32_t ID = rx_frame.MsgID; if (RECV_STATUS_1 == ID) { frametype = "status1"; - vescData.erpm = readInt32Value(rx_frame, 0); - vescData.current = readInt16Value(rx_frame, 4) / 10.0; - vescData.dutyCycle = readInt16Value(rx_frame, 6); + vescData->erpm = readInt32Value(rx_frame, 0); + vescData->current = readInt16Value(rx_frame, 4) / 10.0; + vescData->dutyCycle = readInt16Value(rx_frame, 6); } if (RECV_STATUS_2 == ID) { frametype = "status2"; - vescData.ampHours = readInt32Value(rx_frame, 0) / 10000.0; - vescData.ampHoursCharged = readInt32Value(rx_frame, 4) / 10000.0; + vescData->ampHours = readInt32Value(rx_frame, 0) / 10000.0; + vescData->ampHoursCharged = readInt32Value(rx_frame, 4) / 10000.0; } if (RECV_STATUS_3 == ID) { frametype = "status3"; - vescData.wattHours = readInt32Value(rx_frame, 0) / 10000.0; - vescData.wattHoursCharged = readInt32Value(rx_frame, 4) / 10000.0; + vescData->wattHours = readInt32Value(rx_frame, 0) / 10000.0; + vescData->wattHoursCharged = readInt32Value(rx_frame, 4) / 10000.0; } if (RECV_STATUS_4 == ID) { frametype = "status4"; - vescData.mosfetTemp = readInt16Value(rx_frame, 0) / 10.0; - vescData.motorTemp = readInt16Value(rx_frame, 2) / 10.0; - vescData.totalCurrentIn = readInt16Value(rx_frame, 4) / 10.0; - vescData.pidPosition = readInt16Value(rx_frame, 6) / 50.0; + vescData->mosfetTemp = readInt16Value(rx_frame, 0) / 10.0; + vescData->motorTemp = readInt16Value(rx_frame, 2) / 10.0; + vescData->totalCurrentIn = readInt16Value(rx_frame, 4) / 10.0; + vescData->pidPosition = readInt16Value(rx_frame, 6) / 50.0; } if (RECV_STATUS_5 == ID) { frametype = "status5"; - vescData.tachometer = readInt32Value(rx_frame, 0); - vescData.inputVoltage = readInt16Value(rx_frame, 4) / 10.0; - vescData.inputVoltage += AppConfiguration::getInstance()->config.batteryDrift; + vescData->tachometer = readInt32Value(rx_frame, 0); + vescData->inputVoltage = readInt16Value(rx_frame, 4) / 10.0; + vescData->inputVoltage += AppConfiguration::getInstance()->config.batteryDrift; } if (RECV_PROCESS_SHORT_BUFFER_PROXY == ID) { @@ -246,35 +247,35 @@ void CanBus::processFrame(CAN_frame_t rx_frame, int frameCount) { if (command == 0x00) { frametype += "COMM_FW_VERSION"; int offset = 1; - vescData.majorVersion = readInt8ValueFromBuffer(0, isProxyRequest); - vescData.minorVersion = readInt8ValueFromBuffer(1, isProxyRequest); - vescData.name = readStringValueFromBuffer(2 + offset, 12, isProxyRequest); + vescData->majorVersion = readInt8ValueFromBuffer(0, isProxyRequest); + vescData->minorVersion = readInt8ValueFromBuffer(1, isProxyRequest); + vescData->name = readStringValueFromBuffer(2 + offset, 12, isProxyRequest); } else if (command == 0x4F) { //0x4F = 79 DEC frametype += "COMM_GET_DECODED_BALANCE"; int offset = 1; - vescData.pidOutput = readInt32ValueFromBuffer(0 + offset, isProxyRequest) / 1000000.0; - vescData.pitch = readInt32ValueFromBuffer(4 + offset, isProxyRequest) / 1000000.0; - vescData.roll = readInt32ValueFromBuffer(8 + offset, isProxyRequest) / 1000000.0; - vescData.loopTime = readInt32ValueFromBuffer(12 + offset, isProxyRequest); - vescData.motorCurrent = readInt32ValueFromBuffer(16 + offset, isProxyRequest) / 1000000.0; - vescData.motorPosition = readInt32ValueFromBuffer(20 + offset, isProxyRequest) / 1000000.0; - vescData.balanceState = readInt16ValueFromBuffer(24 + offset, isProxyRequest); - vescData.switchState = readInt16ValueFromBuffer(26 + offset, isProxyRequest); - vescData.adc1 = readInt32ValueFromBuffer(28 + offset, isProxyRequest) / 1000000.0; - vescData.adc2 = readInt32ValueFromBuffer(32 + offset, isProxyRequest) / 1000000.0; + vescData->pidOutput = readInt32ValueFromBuffer(0 + offset, isProxyRequest) / 1000000.0; + vescData->pitch = readInt32ValueFromBuffer(4 + offset, isProxyRequest) / 1000000.0; + vescData->roll = readInt32ValueFromBuffer(8 + offset, isProxyRequest) / 1000000.0; + vescData->loopTime = readInt32ValueFromBuffer(12 + offset, isProxyRequest); + vescData->motorCurrent = readInt32ValueFromBuffer(16 + offset, isProxyRequest) / 1000000.0; + vescData->motorPosition = readInt32ValueFromBuffer(20 + offset, isProxyRequest) / 1000000.0; + vescData->balanceState = readInt16ValueFromBuffer(24 + offset, isProxyRequest); + vescData->switchState = readInt16ValueFromBuffer(26 + offset, isProxyRequest); + vescData->adc1 = readInt32ValueFromBuffer(28 + offset, isProxyRequest) / 1000000.0; + vescData->adc2 = readInt32ValueFromBuffer(32 + offset, isProxyRequest) / 1000000.0; lastBalanceData = millis(); } else if (command == 0x32) { //0x32 = 50 DEC frametype += "COMM_GET_VALUES_SELECTIVE"; int offset = 1; - vescData.mosfetTemp = readInt16ValueFromBuffer(4 + offset, isProxyRequest) / 10.0; - vescData.motorTemp = readInt16ValueFromBuffer(6 + offset, isProxyRequest) / 10.0; - vescData.dutyCycle = readInt16ValueFromBuffer(8 + offset, isProxyRequest) / 1000.0; - vescData.erpm = readInt32ValueFromBuffer(10 + offset, isProxyRequest); - vescData.inputVoltage = readInt16ValueFromBuffer(14 + offset, isProxyRequest) / 10.0; - vescData.inputVoltage += AppConfiguration::getInstance()->config.batteryDrift; - vescData.tachometer = readInt32ValueFromBuffer(16 + offset, isProxyRequest); - vescData.tachometerAbsolut = readInt32ValueFromBuffer(20 + offset, isProxyRequest); - vescData.fault = readInt8ValueFromBuffer(24 + offset, isProxyRequest); + vescData->mosfetTemp = readInt16ValueFromBuffer(4 + offset, isProxyRequest) / 10.0; + vescData->motorTemp = readInt16ValueFromBuffer(6 + offset, isProxyRequest) / 10.0; + vescData->dutyCycle = readInt16ValueFromBuffer(8 + offset, isProxyRequest) / 1000.0; + vescData->erpm = readInt32ValueFromBuffer(10 + offset, isProxyRequest); + vescData->inputVoltage = readInt16ValueFromBuffer(14 + offset, isProxyRequest) / 10.0; + vescData->inputVoltage += AppConfiguration::getInstance()->config.batteryDrift; + vescData->tachometer = readInt32ValueFromBuffer(16 + offset, isProxyRequest); + vescData->tachometerAbsolut = readInt32ValueFromBuffer(20 + offset, isProxyRequest); + vescData->fault = readInt8ValueFromBuffer(24 + offset, isProxyRequest); lastRealtimeData = millis(); } else if (command == 0x33) { //0x33 = 51 DEC frametype += "COMM_GET_VALUES_SETUP_SELECTIVE"; @@ -283,11 +284,11 @@ void CanBus::processFrame(CAN_frame_t rx_frame, int frameCount) { uint32_t bitmask = readInt32ValueFromBuffer(0 + offset, isProxyRequest); startbyte += 4; if(bitmask & ((uint32_t) 1 << 0)) { - vescData.mosfetTemp = readInt16ValueFromBuffer(startbyte + offset, isProxyRequest) / 10.0; + vescData->mosfetTemp = readInt16ValueFromBuffer(startbyte + offset, isProxyRequest) / 10.0; startbyte += 2; } if(bitmask & ((uint32_t) 1 << 1)) { - vescData.motorTemp = readInt16ValueFromBuffer(startbyte + offset, isProxyRequest) / 10.0; + vescData->motorTemp = readInt16ValueFromBuffer(startbyte + offset, isProxyRequest) / 10.0; startbyte += 2; } if(bitmask & ((uint32_t) 1 << 2)) { @@ -299,11 +300,11 @@ void CanBus::processFrame(CAN_frame_t rx_frame, int frameCount) { startbyte += 4; } if(bitmask & ((uint32_t) 1 << 4)) { - vescData.dutyCycle = readInt16ValueFromBuffer(startbyte + offset, isProxyRequest) / 1000.0; + vescData->dutyCycle = readInt16ValueFromBuffer(startbyte + offset, isProxyRequest) / 1000.0; startbyte += 2; } if(bitmask & ((uint32_t) 1 << 5)) { - vescData.erpm = readInt32ValueFromBuffer(startbyte + offset, isProxyRequest); + vescData->erpm = readInt32ValueFromBuffer(startbyte + offset, isProxyRequest); startbyte += 4; } if(bitmask & ((uint32_t) 1 << 6)) { @@ -311,8 +312,8 @@ void CanBus::processFrame(CAN_frame_t rx_frame, int frameCount) { startbyte += 4; } if(bitmask & ((uint32_t) 1 << 7)) { - vescData.inputVoltage = readInt16ValueFromBuffer(startbyte + offset, isProxyRequest) / 10.0; - vescData.inputVoltage += AppConfiguration::getInstance()->config.batteryDrift; + vescData->inputVoltage = readInt16ValueFromBuffer(startbyte + offset, isProxyRequest) / 10.0; + vescData->inputVoltage += AppConfiguration::getInstance()->config.batteryDrift; startbyte += 2; } if(bitmask & ((uint32_t) 1 << 8)) { @@ -336,38 +337,38 @@ void CanBus::processFrame(CAN_frame_t rx_frame, int frameCount) { startbyte += 4; } if(bitmask & ((uint32_t) 1 << 13)) { - vescData.tachometer = readInt32ValueFromBuffer(16 + offset, isProxyRequest); + vescData->tachometer = readInt32ValueFromBuffer(16 + offset, isProxyRequest); startbyte += 4; } if(bitmask & ((uint32_t) 1 << 14)) { - vescData.tachometerAbsolut = readInt32ValueFromBuffer(20 + offset, isProxyRequest); + vescData->tachometerAbsolut = readInt32ValueFromBuffer(20 + offset, isProxyRequest); startbyte += 4; } if(bitmask & ((uint32_t) 1 << 16)) { - vescData.fault = readInt8ValueFromBuffer(24 + offset, isProxyRequest); + vescData->fault = readInt8ValueFromBuffer(24 + offset, isProxyRequest); startbyte += 1; } lastRealtimeData = millis(); } else if (command == 0x04) { frametype += "COMM_GET_VALUES"; int offset = 1; - vescData.mosfetTemp = readInt16ValueFromBuffer(0 + offset, isProxyRequest) / 10.0; - vescData.motorTemp = readInt16ValueFromBuffer(2 + offset, isProxyRequest) / 10.0; - vescData.motorCurrent = readInt32ValueFromBuffer(4 + offset, isProxyRequest) / 100.0; - vescData.current = readInt32ValueFromBuffer(8 + offset, isProxyRequest) / 100.0; - // id = vescData.readInt32ValueFromBuffer(12 + offset, isProxyRequest) / 100.0; - // iq = vescData.readInt32ValueFromBuffer(16 + offset, isProxyRequest) / 100.0; - vescData.dutyCycle = readInt16ValueFromBuffer(20 + offset, isProxyRequest) / 1000.0; - vescData.erpm = readInt32ValueFromBuffer(22 + offset, isProxyRequest); - vescData.inputVoltage = readInt16ValueFromBuffer(26 + offset, isProxyRequest) / 10.0; - vescData.inputVoltage += AppConfiguration::getInstance()->config.batteryDrift; - vescData.ampHours = readInt32ValueFromBuffer(28 + offset, isProxyRequest) / 10000.0; - vescData.ampHoursCharged = readInt32ValueFromBuffer(32 + offset, isProxyRequest) / 10000.0; - vescData.wattHours = readInt32ValueFromBuffer(46 + offset, isProxyRequest) / 10000.0; - vescData.wattHoursCharged = readInt32ValueFromBuffer(40 + offset, isProxyRequest) / 10000.0; - vescData.tachometer = readInt32ValueFromBuffer(44 + offset, isProxyRequest); - vescData.tachometerAbsolut = readInt32ValueFromBuffer(58 + offset, isProxyRequest); - vescData.fault = readInt8ValueFromBuffer(52 + offset, isProxyRequest); + vescData->mosfetTemp = readInt16ValueFromBuffer(0 + offset, isProxyRequest) / 10.0; + vescData->motorTemp = readInt16ValueFromBuffer(2 + offset, isProxyRequest) / 10.0; + vescData->motorCurrent = readInt32ValueFromBuffer(4 + offset, isProxyRequest) / 100.0; + vescData->current = readInt32ValueFromBuffer(8 + offset, isProxyRequest) / 100.0; + // id = vescData->readInt32ValueFromBuffer(12 + offset, isProxyRequest) / 100.0; + // iq = vescData->readInt32ValueFromBuffer(16 + offset, isProxyRequest) / 100.0; + vescData->dutyCycle = readInt16ValueFromBuffer(20 + offset, isProxyRequest) / 1000.0; + vescData->erpm = readInt32ValueFromBuffer(22 + offset, isProxyRequest); + vescData->inputVoltage = readInt16ValueFromBuffer(26 + offset, isProxyRequest) / 10.0; + vescData->inputVoltage += AppConfiguration::getInstance()->config.batteryDrift; + vescData->ampHours = readInt32ValueFromBuffer(28 + offset, isProxyRequest) / 10000.0; + vescData->ampHoursCharged = readInt32ValueFromBuffer(32 + offset, isProxyRequest) / 10000.0; + vescData->wattHours = readInt32ValueFromBuffer(46 + offset, isProxyRequest) / 10000.0; + vescData->wattHoursCharged = readInt32ValueFromBuffer(40 + offset, isProxyRequest) / 10000.0; + vescData->tachometer = readInt32ValueFromBuffer(44 + offset, isProxyRequest); + vescData->tachometerAbsolut = readInt32ValueFromBuffer(58 + offset, isProxyRequest); + vescData->fault = readInt8ValueFromBuffer(52 + offset, isProxyRequest); lastRealtimeData = millis(); } else if (command == 0x0E) { //0x0E = 14 DEC frametype += "COMM_GET_MCCONF"; @@ -407,73 +408,73 @@ void CanBus::dumpVescValues() { char val[size]; std::string bufferString; bufferString += "name="; - snprintf(val, size, "%s, ", vescData.name.c_str()); + snprintf(val, size, "%s, ", vescData->name.c_str()); bufferString += val; bufferString += "dutycycle="; - snprintf(val, size, "%f", vescData.dutyCycle); + snprintf(val, size, "%f", vescData->dutyCycle); bufferString += val; bufferString += ", erpm="; - snprintf(val, size, "%f", vescData.erpm); + snprintf(val, size, "%f", vescData->erpm); bufferString += val; bufferString += ", current="; - snprintf(val, size, "%f", vescData.current); + snprintf(val, size, "%f", vescData->current); bufferString += val; bufferString += ", ampHours="; - snprintf(val, size, "%f", vescData.ampHours); + snprintf(val, size, "%f", vescData->ampHours); bufferString += val; bufferString += ", ampHoursCharged="; - snprintf(val, size, "%f", vescData.ampHoursCharged); + snprintf(val, size, "%f", vescData->ampHoursCharged); bufferString += val; bufferString += ", wattHours="; - snprintf(val, size, "%f", vescData.wattHours); + snprintf(val, size, "%f", vescData->wattHours); bufferString += val; bufferString += ", wattHoursCharged="; - snprintf(val, size, "%f", vescData.wattHoursCharged); + snprintf(val, size, "%f", vescData->wattHoursCharged); bufferString += val; bufferString += ", mosfetTemp="; - snprintf(val, size, "%f", vescData.mosfetTemp); + snprintf(val, size, "%f", vescData->mosfetTemp); bufferString += val; bufferString += ", motorTemp="; - snprintf(val, size, "%f", vescData.motorTemp); + snprintf(val, size, "%f", vescData->motorTemp); bufferString += val; bufferString += ", inputVoltage="; - snprintf(val, size, "%f", vescData.inputVoltage); + snprintf(val, size, "%f", vescData->inputVoltage); bufferString += val; bufferString += ", tachometer="; - snprintf(val, size, "%f", vescData.tachometer); + snprintf(val, size, "%f", vescData->tachometer); bufferString += val; bufferString += ", pidOutput="; - snprintf(val, size, "%f", vescData.pidOutput); + snprintf(val, size, "%f", vescData->pidOutput); bufferString += val; bufferString += ", pitch="; - snprintf(val, size, "%f", vescData.pitch); + snprintf(val, size, "%f", vescData->pitch); bufferString += val; bufferString += ", roll="; - snprintf(val, size, "%f", vescData.roll); + snprintf(val, size, "%f", vescData->roll); bufferString += val; bufferString += ", loopTime="; - snprintf(val, size, "%d", vescData.loopTime); + snprintf(val, size, "%d", vescData->loopTime); bufferString += val; bufferString += ", motorCurrent="; - snprintf(val, size, "%f", vescData.motorCurrent); + snprintf(val, size, "%f", vescData->motorCurrent); bufferString += val; bufferString += ", motorPosition="; - snprintf(val, size, "%f", vescData.motorPosition); + snprintf(val, size, "%f", vescData->motorPosition); bufferString += val; bufferString += ", balanceState="; - snprintf(val, size, "%d", vescData.balanceState); + snprintf(val, size, "%d", vescData->balanceState); bufferString += val; bufferString += ", switchState="; - snprintf(val, size, "%d", vescData.switchState); + snprintf(val, size, "%d", vescData->switchState); bufferString += val; bufferString += ", adc1="; - snprintf(val, size, "%f", vescData.adc1); + snprintf(val, size, "%f", vescData->adc1); bufferString += val; bufferString += ", adc2="; - snprintf(val, size, "%f", vescData.adc2); + snprintf(val, size, "%f", vescData->adc2); bufferString += val; bufferString += ", fault="; - snprintf(val, size, "%d", vescData.fault); + snprintf(val, size, "%d", vescData->fault); bufferString += val; Logger::verbose(LOG_TAG_CANBUS, bufferString.c_str()); lastDump = millis(); diff --git a/src/CanBus.h b/src/CanBus.h index 5c87292..b70737b 100644 --- a/src/CanBus.h +++ b/src/CanBus.h @@ -17,6 +17,7 @@ #include "VescCanConstants.h" #include "BleCanProxy.h" #include "CanDevice.h" +#include "VescData.h" #include #define B10000001 129 @@ -26,41 +27,8 @@ class CanBus { public: - struct VescData { - uint8_t majorVersion; - uint8_t minorVersion; - std::string name; - std::string uuid; - - double dutyCycle = 0.0; - double erpm = 0.0; - double current = 0.0; - double ampHours = 0.0; - double ampHoursCharged= 0.0; - double wattHours = 0.0; - double wattHoursCharged = 0.0; - double mosfetTemp = 0.0; - double motorTemp = 0.0; - double totalCurrentIn = 0.0; - double pidPosition = 0.0; - double inputVoltage = 0.0; - double tachometer = 0.0; - double tachometerAbsolut = 0.0; - - double pidOutput = 0.0; - double pitch = 0.0; - double roll = 0.0; - uint32_t loopTime = 0; - double motorCurrent = 0.0; - double motorPosition = 0.0; - uint16_t balanceState = 0; - uint16_t switchState = 0; - double adc1 = 0.0; - double adc2 = 0.0; - uint8_t fault = 0; - } vescData; - - CanBus(); + CanBus(VescData *vescData); + VescData *vescData; LoopbackStream *stream; BleCanProxy *proxy; void init(); diff --git a/src/ILedController.h b/src/ILedController.h index 0e8876d..0322e09 100644 --- a/src/ILedController.h +++ b/src/ILedController.h @@ -3,6 +3,7 @@ #include #include "CanBus.h" +#include "VescData.h" #define LOG_TAG_LED "ILedController" @@ -32,7 +33,7 @@ class LedControllerFactory { public: static LedControllerFactory* getInstance(); static uint8_t determineLedType(); - ILedController* createLedController(CanBus::VescData *vescData); + ILedController* createLedController(VescData *vescData); private: LedControllerFactory(); diff --git a/src/LedControllerFactory.cpp b/src/LedControllerFactory.cpp index e0620be..8c2c1b0 100644 --- a/src/LedControllerFactory.cpp +++ b/src/LedControllerFactory.cpp @@ -14,7 +14,7 @@ LedControllerFactory *LedControllerFactory::getInstance() { return instance; } -ILedController *LedControllerFactory::createLedController(CanBus::VescData *vescData) { +ILedController *LedControllerFactory::createLedController(VescData *vescData) { #ifdef LED_WS28xx uint8_t ledType = LedControllerFactory::determineLedType(); return new Ws28xxController(AppConfiguration::getInstance()->config.numberPixelLight, PIN_NEOPIXEL, ledType, vescData); diff --git a/src/LightBarController.cpp b/src/LightBarController.cpp index 0bef764..018ab14 100644 --- a/src/LightBarController.cpp +++ b/src/LightBarController.cpp @@ -9,6 +9,7 @@ int pixel_count = 0; int min_voltage = 0; int max_voltage = 0; int voltage_range = 0; +boolean pixelCountOdd = false; AdcState lastAdcState = AdcState::ADC_NONE; unsigned long lastAdcStateChange = 0; @@ -20,6 +21,7 @@ LightBarController *LightBarController::getInstance() { pixel_count = AppConfiguration::getInstance()->config.numberPixelBatMon; min_voltage = AppConfiguration::getInstance()->config.minBatteryVoltage * 100; max_voltage = AppConfiguration::getInstance()->config.maxBatteryVoltage * 100; + pixelCountOdd = pixel_count % 2 == 1; uint8_t ledType = LedControllerFactory::getInstance()->determineLedType(); voltage_range = max_voltage - min_voltage; lightPixels.updateLength(pixel_count); @@ -68,7 +70,7 @@ void LightBarController::updateLightBar(float voltage, AdcState adcState, double lightPixels.setPixelColor(i, 153, 0, 153); // full purple break; case ADC_HALF_ADC1: - if (i > (pixel_count / 2)) { + if ((pixelCountOdd && i > (pixel_count / 2)) || (!pixelCountOdd &&i >= (pixel_count / 2))) { lightPixels.setPixelColor(i, 153, 0, 153); // half purple } break; diff --git a/src/VescData.h b/src/VescData.h new file mode 100644 index 0000000..876182f --- /dev/null +++ b/src/VescData.h @@ -0,0 +1,44 @@ +// +// Created by David Anthony Gey on 14.10.22. +// + +#ifndef RESCUE_VESCDATA_H +#define RESCUE_VESCDATA_H + +#include + +struct VescData { + uint8_t majorVersion; + uint8_t minorVersion; + std::string name; + std::string uuid; + + double dutyCycle = 0.0; + double erpm = 0.0; + double current = 0.0; + double ampHours = 0.0; + double ampHoursCharged= 0.0; + double wattHours = 0.0; + double wattHoursCharged = 0.0; + double mosfetTemp = 0.0; + double motorTemp = 0.0; + double totalCurrentIn = 0.0; + double pidPosition = 0.0; + double inputVoltage = 0.0; + double tachometer = 0.0; + double tachometerAbsolut = 0.0; + + double pidOutput = 0.0; + double pitch = 0.0; + double roll = 0.0; + uint32_t loopTime = 0; + double motorCurrent = 0.0; + double motorPosition = 0.0; + uint16_t balanceState = 0; + uint16_t switchState = 0; + double adc1 = 0.0; + double adc2 = 0.0; + uint8_t fault = 0; +}; + +#endif //RESCUE_VESCDATA_H diff --git a/src/Ws28xxController.cpp b/src/Ws28xxController.cpp index 866ee4b..19631ca 100644 --- a/src/Ws28xxController.cpp +++ b/src/Ws28xxController.cpp @@ -1,7 +1,7 @@ #include "Ws28xxController.h" #include -Ws28xxController::Ws28xxController(uint16_t pixels, uint8_t pin, uint8_t type, CanBus::VescData *vescData) +Ws28xxController::Ws28xxController(uint16_t pixels, uint8_t pin, uint8_t type, VescData *vescData) : Adafruit_NeoPixel(pixels, pin, type) { this->vescData = vescData; } @@ -123,9 +123,6 @@ void Ws28xxController::changePattern(Pattern pattern, boolean isForward, boolean Logger::verbose(LOG_TAG_WS28XX, buf); } - clear(); - show(); - maxBrightness = config.lightMaxBrightness; stopPattern = false; repeat = repeatPattern; @@ -141,7 +138,7 @@ void Ws28xxController::changePattern(Pattern pattern, boolean isForward, boolean (config.lightColorPrimaryBlue * maxBrightness) >> 8), Color((config.lightColorSecondaryRed * maxBrightness) >> 8, (config.lightColorSecondaryGreen * maxBrightness) >> 8, - (config.lightColorSecondaryBlue * maxBrightness) >> 8), 400); + (config.lightColorSecondaryBlue * maxBrightness) >> 8), (uint8_t)400); break; case COLOR_WIPE: break; @@ -172,6 +169,8 @@ void Ws28xxController::changePattern(Pattern pattern, boolean isForward, boolean config.startLightDuration / (numPixels() / 4)); break; case BATTERY_INDICATOR: + clear(); + show(); batteryIndicator(1000); break; case NONE: diff --git a/src/Ws28xxController.h b/src/Ws28xxController.h index 462becb..81bbcf0 100644 --- a/src/Ws28xxController.h +++ b/src/Ws28xxController.h @@ -15,7 +15,7 @@ class Ws28xxController : public ILedController, Adafruit_NeoPixel { public: - Ws28xxController(uint16_t pixels, uint8_t pin, uint8_t type, CanBus::VescData *vescData); + Ws28xxController(uint16_t pixels, uint8_t pin, uint8_t type, VescData *vescData); void init() override; void stop() override; void startSequence() override; @@ -68,6 +68,6 @@ class Ws28xxController : public ILedController, Adafruit_NeoPixel { boolean up = false; int maxBrightness = MAX_BRIGHTNESS; Config config = AppConfiguration::getInstance()->config; - CanBus::VescData *vescData; + VescData *vescData; }; #endif //__LED_CONTROLLER_H__ \ No newline at end of file diff --git a/src/config.h b/src/config.h index ec10fda..5d30040 100644 --- a/src/config.h +++ b/src/config.h @@ -42,21 +42,13 @@ #define VESC_BAUD_RATE 115200 // BAUD rate of the CF2 -// The name this controller should advertise for BLE #define CANBUS_ENABLED -#define BLYNK_ENABLED #ifdef CANBUS_ENABLED #define VESC_CAN_ID 25 //VESC-ID as configured in VESC as decimal #define CANBUS_ONLY #endif //CANBUS_ENABLED -#ifdef BLYNK_ENABLED - // Blynk token for BLE - //#define BLYNK_AUTH_TOKEN "l7b1ongYuGiRcnDHufxwnazMH6hAn3Xl" - #define BLYNK_AUTH_TOKEN "bb48a7193d764ea7a90dbffb54ffcb76" -#endif - ////#define FAKE_VESC_ENABLED #endif //__CONFIG_H__ diff --git a/src/main.cpp b/src/main.cpp index 1f332af..29d5d14 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,44 +21,45 @@ double idle_erpm = 10.0; int lastFake = 4000; int lastFakeCount = 0; +VescData vescData; HardwareSerial vesc(2); ILedController *ledController; #if defined(CANBUS_ENABLED) - CanBus * canbus = new CanBus(); - BatteryMonitor *batMonitor = new BatteryMonitor(&canbus->vescData); -#else - BatteryMonitor *batMonitor = new BatteryMonitor(); + CanBus * canbus = new CanBus(&vescData); #endif //CANBUS_ENABLED + BatteryMonitor *batMonitor = new BatteryMonitor(&vescData); BleServer *bleServer = new BleServer(); // Declare the local logger function before it is called. void localLogger(Logger::Level level, const char* module, const char* message); +#if defined(CANBUS_ENABLED) void fakeCanbusValues() { if(millis() - lastFake > 3000) { - canbus->vescData.tachometer= random(0, 30); - canbus->vescData.inputVoltage = random(43, 50); - canbus->vescData.dutyCycle = random(0, 100); + vescData.tachometer= random(0, 30); + vescData.inputVoltage = random(43, 50); + vescData.dutyCycle = random(0, 100); if(lastFakeCount > 10) { - canbus->vescData.erpm = random(-100, 200); + vescData.erpm = random(-100, 200); } else { - canbus->vescData.erpm = 0;//random(-100, 200); + vescData.erpm = 0;//random(-100, 200); } - canbus->vescData.current = random(0, 10); - canbus->vescData.ampHours = random(0, 100); - canbus->vescData.mosfetTemp = random(20, 60); - canbus->vescData.motorTemp = random(20, 40); - canbus->vescData.adc1 = 0.5; - canbus->vescData.adc2 = 0.5; - canbus->vescData.switchState = 0; + vescData.current = random(0, 10); + vescData.ampHours = random(0, 100); + vescData.mosfetTemp = random(20, 60); + vescData.motorTemp = random(20, 40); + vescData.adc1 = 0.5; + vescData.adc2 = 0.5; + vescData.switchState = 0; lastFake = millis(); lastFakeCount++; } } +#endif void setup() { Logger::setOutputFunction(localLogger); @@ -74,7 +75,7 @@ void setup() { return; } - ledController = LedControllerFactory::getInstance()->createLedController(&canbus->vescData); + ledController = LedControllerFactory::getInstance()->createLedController(&vescData); pinMode(PIN_FORWARD, INPUT); pinMode(PIN_BACKWARD, INPUT); @@ -93,7 +94,7 @@ void setup() { #ifdef CANBUS_ONLY bleServer->init(canbus->stream, canbus); #else - bleServer->init(&vesc, canbus); + bleServer->init(&vesc); #endif // initialize the LED (either COB or Neopixel) ledController->init(); @@ -131,15 +132,15 @@ void loop() { fakeCanbusValues(); #endif - new_forward = canbus->vescData.erpm > idle_erpm ? HIGH : LOW; - new_backward = canbus->vescData.erpm < -idle_erpm ? HIGH : LOW; - idle = (abs(canbus->vescData.erpm) < idle_erpm && canbus->vescData.switchState == 0) ? HIGH : LOW; - new_brake = (abs(canbus->vescData.erpm) > idle_erpm && canbus->vescData.current < -4.0) ? HIGH : LOW; + new_forward = vescData.erpm > idle_erpm ? HIGH : LOW; + new_backward = vescData.erpm < -idle_erpm ? HIGH : LOW; + idle = (abs(vescData.erpm) < idle_erpm && vescData.switchState == 0) ? HIGH : LOW; + new_brake = (abs(vescData.erpm) > idle_erpm && vescData.current < -4.0) ? HIGH : LOW; #else new_forward = digitalRead(PIN_FORWARD); new_backward = digitalRead(PIN_BACKWARD); new_brake = digitalRead(PIN_BRAKE); - idle = *(new_forward) == LOW && *(new_backward) == LOW; + idle = new_forward == LOW && new_backward == LOW; #endif #ifdef CANBUS_ENABLED @@ -162,7 +163,7 @@ void loop() { // call the VESC UART-to-Bluetooth bridge #ifdef CANBUS_ENABLED - bleServer->loop(&canbus->vescData, loopTime, maxLoopTime); + bleServer->loop(&vescData, loopTime, maxLoopTime); #else bleServer->loop(); #endif