Monitor motorhome leisure battery and provide simple control of habitation
Dependencies: net lpc1768 crypto clock web fram log
motorhome/comms.c@10:a97a7cb7aa82, 24 months ago (annotated)
- Committer:
- andrewboyson
- Date:
- Fri Jun 10 18:32:21 2022 +0000
- Revision:
- 10:a97a7cb7aa82
- Parent:
- 9:d957af50fdc2
Corrected amp second count in html
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 9:d957af50fdc2 | 1 | #include <stdint.h> |
andrewboyson | 9:d957af50fdc2 | 2 | |
andrewboyson | 9:d957af50fdc2 | 3 | #include "can.h" |
andrewboyson | 9:d957af50fdc2 | 4 | #include "mstimer.h" |
andrewboyson | 9:d957af50fdc2 | 5 | #include "log.h" |
andrewboyson | 9:d957af50fdc2 | 6 | #include "comms.h" |
andrewboyson | 9:d957af50fdc2 | 7 | |
andrewboyson | 9:d957af50fdc2 | 8 | #define CAN_ID_BATTERY 0x100 |
andrewboyson | 9:d957af50fdc2 | 9 | #define CAN_ID_COUNTED_AMP_SECONDS 0x00 |
andrewboyson | 9:d957af50fdc2 | 10 | #define CAN_ID_COUNTED_ERROR_PERCENT 0x01 |
andrewboyson | 9:d957af50fdc2 | 11 | #define CAN_ID_MA 0x02 |
andrewboyson | 9:d957af50fdc2 | 12 | #define CAN_ID_OUTPUT_SETPOINT 0x03 |
andrewboyson | 9:d957af50fdc2 | 13 | #define CAN_ID_OUTPUT_STATE 0x04 |
andrewboyson | 9:d957af50fdc2 | 14 | #define CAN_ID_CHARGE_ENABLED 0x05 |
andrewboyson | 9:d957af50fdc2 | 15 | #define CAN_ID_DISCHARGE_ENABLED 0x06 |
andrewboyson | 9:d957af50fdc2 | 16 | #define CAN_ID_TEMPERATURE_TENTHS 0x07 |
andrewboyson | 9:d957af50fdc2 | 17 | #define CAN_ID_HEATER_SET_POINT 0x08 |
andrewboyson | 9:d957af50fdc2 | 18 | #define CAN_ID_HEATER_OUTPUT 0x09 |
andrewboyson | 9:d957af50fdc2 | 19 | #define CAN_ID_VOLTAGE 0x0A |
andrewboyson | 9:d957af50fdc2 | 20 | #define CAN_ID_MEASURED_SOC_PERCENT 0x0B |
andrewboyson | 9:d957af50fdc2 | 21 | #define CAN_ID_MEASURED_SOC_ERROR 0x0C |
andrewboyson | 9:d957af50fdc2 | 22 | #define CAN_ID_CAPACITY_AUTO_UPDATE 0x0D |
andrewboyson | 9:d957af50fdc2 | 23 | |
andrewboyson | 9:d957af50fdc2 | 24 | |
andrewboyson | 9:d957af50fdc2 | 25 | static uint32_t _batteryCountedCapacity = 0; |
andrewboyson | 9:d957af50fdc2 | 26 | static uint8_t _batteryCountedError = 0; |
andrewboyson | 9:d957af50fdc2 | 27 | static int32_t _batteryCurrentMa = 0; |
andrewboyson | 9:d957af50fdc2 | 28 | static uint8_t _batteryCapacitySetPointPercent = 0; |
andrewboyson | 9:d957af50fdc2 | 29 | static char _batteryOutputState = ' '; |
andrewboyson | 9:d957af50fdc2 | 30 | static char _batteryChargeEnabled = 0; |
andrewboyson | 9:d957af50fdc2 | 31 | static char _batteryDischargeEnabled = 0; |
andrewboyson | 9:d957af50fdc2 | 32 | static int16_t _batteryTemperatureTenths = 0; |
andrewboyson | 9:d957af50fdc2 | 33 | static int16_t _batteryTemperatureSetPoint = 0; |
andrewboyson | 9:d957af50fdc2 | 34 | static uint8_t _batteryHeaterOutputPercent = 0; |
andrewboyson | 9:d957af50fdc2 | 35 | static int16_t _batteryVoltageMv = 0; |
andrewboyson | 9:d957af50fdc2 | 36 | static uint8_t _batteryMeasuredPercent = 0; |
andrewboyson | 9:d957af50fdc2 | 37 | static uint8_t _batteryMeasuredError = 0; |
andrewboyson | 9:d957af50fdc2 | 38 | static char _batteryCapacityAutoUpdate = 0; |
andrewboyson | 9:d957af50fdc2 | 39 | |
andrewboyson | 9:d957af50fdc2 | 40 | uint32_t BatteryGetCountedCapacity (){ return _batteryCountedCapacity; } |
andrewboyson | 9:d957af50fdc2 | 41 | uint8_t BatteryGetCountedError (){ return _batteryCountedError; } |
andrewboyson | 9:d957af50fdc2 | 42 | int32_t BatteryGetCurrentMa (){ return _batteryCurrentMa; } |
andrewboyson | 9:d957af50fdc2 | 43 | uint8_t BatteryGetCapacitySetPointPercent (){ return _batteryCapacitySetPointPercent; } |
andrewboyson | 9:d957af50fdc2 | 44 | char BatteryGetOutputState (){ return _batteryOutputState; } |
andrewboyson | 9:d957af50fdc2 | 45 | char BatteryGetChargeEnabled (){ return _batteryChargeEnabled; } |
andrewboyson | 9:d957af50fdc2 | 46 | char BatteryGetDischargeEnabled (){ return _batteryDischargeEnabled; } |
andrewboyson | 9:d957af50fdc2 | 47 | int16_t BatteryGetTemperatureTenths (){ return _batteryTemperatureTenths; } |
andrewboyson | 9:d957af50fdc2 | 48 | int16_t BatteryGetTemperatureSetPoint (){ return _batteryTemperatureSetPoint; } |
andrewboyson | 9:d957af50fdc2 | 49 | uint8_t BatteryGetHeaterOutputPercent (){ return _batteryHeaterOutputPercent; } |
andrewboyson | 9:d957af50fdc2 | 50 | int16_t BatteryGetVoltageMv (){ return _batteryVoltageMv; } |
andrewboyson | 9:d957af50fdc2 | 51 | uint8_t BatteryGetMeasuredPercent (){ return _batteryMeasuredPercent; } |
andrewboyson | 9:d957af50fdc2 | 52 | uint8_t BatteryGetMeasuredError (){ return _batteryMeasuredError; } |
andrewboyson | 9:d957af50fdc2 | 53 | char BatteryGetCapacityAutoUpdate (){ return _batteryCapacityAutoUpdate; } |
andrewboyson | 9:d957af50fdc2 | 54 | |
andrewboyson | 9:d957af50fdc2 | 55 | void BatterySetCountedCapacity (uint32_t value){ _batteryCountedCapacity = value; CanSend(CAN_ID_BATTERY + CAN_ID_COUNTED_AMP_SECONDS , 4, value, 0);} |
andrewboyson | 9:d957af50fdc2 | 56 | void BatterySetCountedError (uint8_t value){ _batteryCountedError = value; CanSend(CAN_ID_BATTERY + CAN_ID_COUNTED_ERROR_PERCENT, 1, value, 0);} |
andrewboyson | 9:d957af50fdc2 | 57 | void BatterySetCurrentMa (int32_t value){ _batteryCurrentMa = value; CanSend(CAN_ID_BATTERY + CAN_ID_MA , 4, value, 0);} |
andrewboyson | 9:d957af50fdc2 | 58 | void BatterySetCapacitySetPointPercent(uint8_t value){ _batteryCapacitySetPointPercent = value; CanSend(CAN_ID_BATTERY + CAN_ID_OUTPUT_SETPOINT , 1, value, 0);} |
andrewboyson | 9:d957af50fdc2 | 59 | void BatterySetOutputState (char value){ _batteryOutputState = value; CanSend(CAN_ID_BATTERY + CAN_ID_OUTPUT_STATE , 1, value, 0);} |
andrewboyson | 9:d957af50fdc2 | 60 | void BatterySetChargeEnabled (char value){ _batteryChargeEnabled = value; CanSend(CAN_ID_BATTERY + CAN_ID_CHARGE_ENABLED , 1, value, 0);} |
andrewboyson | 9:d957af50fdc2 | 61 | void BatterySetDischargeEnabled (char value){ _batteryDischargeEnabled = value; CanSend(CAN_ID_BATTERY + CAN_ID_DISCHARGE_ENABLED , 1, value, 0);} |
andrewboyson | 9:d957af50fdc2 | 62 | void BatterySetTemperatureSetPoint (int16_t value){ _batteryTemperatureSetPoint = value; CanSend(CAN_ID_BATTERY + CAN_ID_HEATER_SET_POINT , 2, value, 0);} |
andrewboyson | 9:d957af50fdc2 | 63 | void BatterySetCapacityAutoUpdate (char value){ _batteryCapacityAutoUpdate = value; CanSend(CAN_ID_BATTERY + CAN_ID_CAPACITY_AUTO_UPDATE , 1, value, 0);} |
andrewboyson | 9:d957af50fdc2 | 64 | |
andrewboyson | 9:d957af50fdc2 | 65 | static void receive(uint16_t id, int length, uint32_t dataA, uint32_t dataB) |
andrewboyson | 9:d957af50fdc2 | 66 | { |
andrewboyson | 9:d957af50fdc2 | 67 | switch(id) |
andrewboyson | 9:d957af50fdc2 | 68 | { |
andrewboyson | 9:d957af50fdc2 | 69 | case CAN_ID_BATTERY + CAN_ID_COUNTED_AMP_SECONDS: _batteryCountedCapacity = (uint32_t)dataA; break; |
andrewboyson | 9:d957af50fdc2 | 70 | case CAN_ID_BATTERY + CAN_ID_COUNTED_ERROR_PERCENT: _batteryCountedError = ( uint8_t)dataA; break; |
andrewboyson | 9:d957af50fdc2 | 71 | case CAN_ID_BATTERY + CAN_ID_MA: _batteryCurrentMa = ( int32_t)dataA; break; |
andrewboyson | 9:d957af50fdc2 | 72 | case CAN_ID_BATTERY + CAN_ID_OUTPUT_SETPOINT: _batteryCapacitySetPointPercent = ( uint8_t)dataA; break; |
andrewboyson | 9:d957af50fdc2 | 73 | case CAN_ID_BATTERY + CAN_ID_OUTPUT_STATE: _batteryOutputState = ( char)dataA; break; |
andrewboyson | 9:d957af50fdc2 | 74 | case CAN_ID_BATTERY + CAN_ID_CHARGE_ENABLED: _batteryChargeEnabled = ( char)dataA; break; |
andrewboyson | 9:d957af50fdc2 | 75 | case CAN_ID_BATTERY + CAN_ID_DISCHARGE_ENABLED: _batteryDischargeEnabled = ( char)dataA; break; |
andrewboyson | 9:d957af50fdc2 | 76 | case CAN_ID_BATTERY + CAN_ID_TEMPERATURE_TENTHS: _batteryTemperatureTenths = ( int16_t)dataA; break; |
andrewboyson | 9:d957af50fdc2 | 77 | case CAN_ID_BATTERY + CAN_ID_HEATER_SET_POINT: _batteryTemperatureSetPoint = ( int16_t)dataA; break; |
andrewboyson | 9:d957af50fdc2 | 78 | case CAN_ID_BATTERY + CAN_ID_HEATER_OUTPUT: _batteryHeaterOutputPercent = ( uint8_t)dataA; break; |
andrewboyson | 9:d957af50fdc2 | 79 | case CAN_ID_BATTERY + CAN_ID_VOLTAGE: _batteryVoltageMv = ( int16_t)dataA; break; |
andrewboyson | 9:d957af50fdc2 | 80 | case CAN_ID_BATTERY + CAN_ID_MEASURED_SOC_PERCENT: _batteryMeasuredPercent = ( uint8_t)dataA; break; |
andrewboyson | 9:d957af50fdc2 | 81 | case CAN_ID_BATTERY + CAN_ID_MEASURED_SOC_ERROR: _batteryMeasuredError = ( uint8_t)dataA; break; |
andrewboyson | 9:d957af50fdc2 | 82 | case CAN_ID_BATTERY + CAN_ID_CAPACITY_AUTO_UPDATE: _batteryCapacityAutoUpdate = ( char)dataA; break; |
andrewboyson | 9:d957af50fdc2 | 83 | default: |
andrewboyson | 9:d957af50fdc2 | 84 | LogTimeF("Unknown CAN message id %03X received containing %d bytes: %0*X\r\n", id, length, length*2, dataA); |
andrewboyson | 9:d957af50fdc2 | 85 | break; |
andrewboyson | 9:d957af50fdc2 | 86 | } |
andrewboyson | 9:d957af50fdc2 | 87 | } |
andrewboyson | 9:d957af50fdc2 | 88 | void CommsInit() |
andrewboyson | 9:d957af50fdc2 | 89 | { |
andrewboyson | 9:d957af50fdc2 | 90 | CanReceive = &receive; |
andrewboyson | 9:d957af50fdc2 | 91 | } |
andrewboyson | 9:d957af50fdc2 | 92 | void CommsMain() |
andrewboyson | 9:d957af50fdc2 | 93 | { |
andrewboyson | 9:d957af50fdc2 | 94 | } |