From 781d83297e5cde25263c3c368c0da0be07f89768 Mon Sep 17 00:00:00 2001 From: didinst <9f6129a2ddff0d0c6d03169eaa75e3fea1c1770f> Date: Sun, 24 Jul 2022 19:32:15 +0300 Subject: [PATCH] Events instead raw frames Signed-off-by: didinst <9f6129a2ddff0d0c6d03169eaa75e3fea1c1770f> --- chademo.go | 302 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 212 insertions(+), 90 deletions(-) diff --git a/chademo.go b/chademo.go index 5d09c19..8bb0678 100644 --- a/chademo.go +++ b/chademo.go @@ -9,29 +9,29 @@ const ( CAN_ID_101 = 101 CAN_ID_102 = 102 - DISABLED VehicleCharging = iota + DISABLED VehicleChargingEnabled = iota ENABLED - PARKING ShiftLever = iota + PARKING ShiftLeverPosition = iota OTHER NORMAL SystemFault = iota FAULT - CONTACTOR_CLOSED VehicleStatus = iota + CONTACTOR_CLOSED ContactorVehicleStatus = iota CONTACTOR_OPEN NO_REQUEST StopRequest = iota STOP_REQUEST ) -type VehicleCharging uint8 +type VehicleChargingEnabled uint8 -type ShiftLever uint8 +type ShiftLeverPosition uint8 type SystemFault uint8 -type VehicleStatus uint8 +type ContactorVehicleStatus uint8 type StopRequest uint8 @@ -39,47 +39,69 @@ type ChademoEvent interface { GetValue() interface{} } -type MaximumBatteryVoltage interface { - // GetMaxBattVoltage Maximum battery voltage - GetMaxBattVoltage() uint16 +type frame struct { + *CanFrame } -type ConstChargingRateInd interface { - // GetChargingRate Constant of charging rate indication - GetChargingRate() uint8 -} +// MaximumBatteryVoltage The maximum voltage value at the vehicle inlet terminals, at which the station stops charging to protect the vehicle battery +type MaximumBatteryVoltage frame -type frame100 struct { - maxBattVoltage uint16 // The maximum voltage value at the vehicle inlet terminals, at which the station stops charging to protect the vehicle battery - chargingRateInd uint8 // Fixed value for charging rate indication, which is the maximum charging rate (100 %) of vehicle battery - date string -} +// ConstChargingRateInd Fixed value for charging rate indication, which is the maximum charging rate (100 %) of vehicle battery +type ConstChargingRateInd frame -type frame101 struct { - batteryCapacity float32 // Rated capacity of battery - maxChargingTimeS uint16 // Maximum charging time permitted by EV, set by 10 s - maxChargingTimeM uint8 // Maximum charging time permitted by EV, set by minute - estChargingTimeM uint8 // Estimated remaining time before the end of charging calculated by EV - date string -} +// BatteryCapacity Rated capacity of battery +type BatteryCapacity frame -type frame102 struct { - controlProtocolNumber uint8 // Software version of control protocol to which EV corresponds - targetBatteryVoltage uint16 // Target battery voltage ed charging voltage at the vehicle inlet terminals - chargingCurrentReq uint8 // Current value requested by EV during charging - chargingRate uint8 // Charging rate of vehicle battery - vehicleCharging VehicleCharging // Status flag indicating charge permission status of EV - shiftLever ShiftLever // Status flag indicating the shift lever position - chargingSystemFault SystemFault // Status flag indicating Charging system fault a malfunction caused by EV or the station, and detected by EV - vehicleStatus VehicleStatus // Status flag indicating the EV contactor status - normalStopReq StopRequest // Status flag indicating the request of EV to stop charging control - battOvervoltage SystemFault // Status flag indicating whether or not the vehicle battery voltage exceeds the maximum limit specified by EV - battUndervoltage SystemFault // Status flag indicating whether or not the vehicle battery voltage is less than the lower limit specified by EV - battCurrentDeviationErr SystemFault // Status flag indicating whether or not the output current deviates from EV requested current - highBattTemperature SystemFault // Status flag indicating whether or not the temperature of vehicle battery exceeds the maximum limit - battVoltageDeviationErr SystemFault // Status flag indicating whether or not the vehicle battery voltage deviates from the output voltage measured by the station - date string -} +// MaxChargingTimeS Maximum charging time permitted by EV, set by 10 s +type MaxChargingTimeS frame + +// MaxChargingTimeM Maximum charging time permitted by EV, set by minute +type MaxChargingTimeM frame + +// EstChargingTimeM Estimated remaining time before the end of charging calculated by EV +type EstChargingTimeM frame + +// ControlProtocolNumber Software version of control protocol to which EV corresponds +type ControlProtocolNumber frame + +// TargetBatteryVoltage Target battery voltage ed charging voltage at the vehicle inlet terminals +type TargetBatteryVoltage frame + +// ChargingCurrentReq Current value requested by EV during charging +type ChargingCurrentReq frame + +// ChargingRate Charging rate of vehicle battery +type ChargingRate frame + +// VehicleCharging Status flag indicating charge permission status of EV +type VehicleCharging frame + +// ShiftLever Status flag indicating the shift lever position +type ShiftLever frame + +// ChargingSystemFault Status flag indicating Charging system fault a malfunction caused by EV or the station, and detected by EV +type ChargingSystemFault frame + +// VehicleStatus Status flag indicating the EV contactor status +type VehicleStatus frame + +// NormalStopReq Status flag indicating the request of EV to stop charging control +type NormalStopReq frame + +// BattOvervoltage Status flag indicating whether or not the vehicle battery voltage exceeds the maximum limit specified by EV +type BattOvervoltage frame + +// BattUndervoltage Status flag indicating whether or not the vehicle battery voltage is less than the lower limit specified by EV +type BattUndervoltage frame + +// BattCurrentDeviationErr Status flag indicating whether or not the output current deviates from EV requested current +type BattCurrentDeviationErr frame + +// HighBattTemperature Status flag indicating whether or not the temperature of vehicle battery exceeds the maximum limit +type HighBattTemperature frame + +// BattVoltageDeviationErr Status flag indicating whether or not the vehicle battery voltage deviates from the output voltage measured by the station +type BattVoltageDeviationErr frame func FromCanFrames(frames <-chan *CanFrame) <-chan ChademoEvent { events := make(chan ChademoEvent) @@ -90,41 +112,28 @@ func FromCanFrames(frames <-chan *CanFrame) <-chan ChademoEvent { for frame := range frames { switch frame.canid { case CAN_ID_100: - event := &frame100{ - maxBattVoltage: bytesToUint16(frame.payload[4:6]), - chargingRateInd: frame.payload[6], - date: frame.date, - } - events <- event + events <- &MaximumBatteryVoltage{frame} + events <- &ConstChargingRateInd{frame} case CAN_ID_101: - event := &frame101{ - batteryCapacity: 0.1 * float32(bytesToUint16(frame.payload[5:7])), - maxChargingTimeS: 10 * uint16(frame.payload[1]), - maxChargingTimeM: frame.payload[2], - estChargingTimeM: frame.payload[3], - date: frame.date, - } - fmt.Println(event) - //events <- event + events <- &BatteryCapacity{frame} + events <- &MaxChargingTimeS{frame} + events <- &MaxChargingTimeM{frame} + events <- &EstChargingTimeM{frame} case CAN_ID_102: - event := &frame102{ - controlProtocolNumber: frame.payload[0], - targetBatteryVoltage: bytesToUint16(frame.payload[1:3]), - chargingCurrentReq: frame.payload[3], - chargingRate: frame.payload[6], - vehicleCharging: getVehicleCharging(&frame.payload), - shiftLever: getVehicleShiftLever(&frame.payload), - chargingSystemFault: getFault(&frame.payload, 5, 2), - vehicleStatus: getVehicleStatus(&frame.payload), - normalStopReq: getNormalStopReq(&frame.payload), - battOvervoltage: getFault(&frame.payload, 4, 0), - battUndervoltage: getFault(&frame.payload, 4, 1), - battCurrentDeviationErr: getFault(&frame.payload, 4, 2), - highBattTemperature: getFault(&frame.payload, 4, 3), - battVoltageDeviationErr: getFault(&frame.payload, 4, 4), - } - fmt.Println(event) - //events <- event + events <- &ControlProtocolNumber{frame} + events <- &TargetBatteryVoltage{frame} + events <- &ChargingCurrentReq{frame} + events <- &ChargingRate{frame} + events <- &VehicleCharging{frame} + events <- &ShiftLever{frame} + events <- &ChargingSystemFault{frame} + events <- &VehicleStatus{frame} + events <- &NormalStopReq{frame} + events <- &BattOvervoltage{frame} + events <- &BattUndervoltage{frame} + events <- &BattCurrentDeviationErr{frame} + events <- &HighBattTemperature{frame} + events <- &BattVoltageDeviationErr{frame} } } }() @@ -132,14 +141,14 @@ func FromCanFrames(frames <-chan *CanFrame) <-chan ChademoEvent { return events } -func getVehicleShiftLever(bytes *[]byte) ShiftLever { +func getVehicleShiftLever(bytes *[]byte) ShiftLeverPosition { if isBitSet((*bytes)[5], 1) { return OTHER } return PARKING } -func getVehicleStatus(bytes *[]byte) VehicleStatus { +func getVehicleStatus(bytes *[]byte) ContactorVehicleStatus { if isBitSet((*bytes)[5], 3) { return CONTACTOR_OPEN } @@ -160,7 +169,7 @@ func getFault(bytes *[]byte, octet byte, bit byte) SystemFault { return NORMAL } -func getVehicleCharging(bytes *[]uint8) VehicleCharging { +func getVehicleCharging(bytes *[]uint8) VehicleChargingEnabled { if isBitSet((*bytes)[5], 0) { return ENABLED } @@ -188,24 +197,137 @@ func bytesToUint16(bytes []uint8) uint16 { return result } -func (f frame100) GetMaxBattVoltage() uint16 { - return f.maxBattVoltage +func (m MaximumBatteryVoltage) GetValue() interface{} { + return bytesToUint16(m.payload[4:6]) +} + +func (c ConstChargingRateInd) GetValue() interface{} { + return c.payload[6] +} + +func (b BatteryCapacity) GetValue() interface{} { + return 0.1 * float32(bytesToUint16(b.payload[5:7])) +} + +func (m MaxChargingTimeS) GetValue() interface{} { + return 10 * uint16(m.payload[1]) +} + +func (m MaxChargingTimeM) GetValue() interface{} { + return m.payload[2] +} + +func (e EstChargingTimeM) GetValue() interface{} { + return e.payload[3] +} + +func (c ControlProtocolNumber) GetValue() interface{} { + return c.payload[0] } -func (f frame100) GetChargingRate() uint8 { - return f.chargingRateInd +func (t TargetBatteryVoltage) GetValue() interface{} { + return bytesToUint16(t.payload[1:3]) } -func (f frame100) GetValue() interface{} { - return fmt.Sprintf("%v | MaxBattVoltage = %vV, ChargingRate = %v%%", f.date, f.maxBattVoltage, f.chargingRateInd) +func (c ChargingCurrentReq) GetValue() interface{} { + return c.payload[3] } -func (f frame101) GetValue() interface{} { - return fmt.Sprintf("%v | batteryCapacity = %vkWh, maxChargingTime = %vsec, maxChargingTime = %vmin, estChargingTime = %vmin", - f.date, f.batteryCapacity, f.maxChargingTimeS, f.maxChargingTimeM, f.estChargingTimeM) +func (c ChargingRate) GetValue() interface{} { + return c.payload[6] } -func (f frame102) GetValue() interface{} { - //TODO implement me - return "" +func (s ShiftLever) GetValue() interface{} { + return getVehicleShiftLever(&s.payload) +} + +func (v VehicleCharging) GetValue() interface{} { + return getVehicleCharging(&v.payload) +} + +func (c ChargingSystemFault) GetValue() interface{} { + return getFault(&c.payload, 5, 2) +} + +func (v VehicleStatus) GetValue() interface{} { + return getVehicleStatus(&v.payload) +} + +func (n NormalStopReq) GetValue() interface{} { + return getNormalStopReq(&n.payload) +} + +func (b BattOvervoltage) GetValue() interface{} { + return getFault(&b.payload, 4, 0) +} + +func (b BattUndervoltage) GetValue() interface{} { + return getFault(&b.payload, 4, 1) +} + +func (b BattCurrentDeviationErr) GetValue() interface{} { + return getFault(&b.payload, 4, 2) +} + +func (h HighBattTemperature) GetValue() interface{} { + return getFault(&h.payload, 4, 3) +} + +func (b BattVoltageDeviationErr) GetValue() interface{} { + return getFault(&b.payload, 4, 4) +} + +func (v VehicleChargingEnabled) String() string { + switch v { + case DISABLED: + return "disabled" + case ENABLED: + return "enabled" + default: + panic("VehicleChargingEnabled not implemented") + } +} + +func (s ShiftLeverPosition) String() string { + switch s { + case PARKING: + return "parking" + case OTHER: + return "other" + default: + panic("ShiftLeverPosition not implemented") + } +} + +func (s SystemFault) String() string { + switch s { + case NORMAL: + return "normal" + case FAULT: + return "fault" + default: + panic("SystemFault not implemented") + } +} + +func (c ContactorVehicleStatus) String() string { + switch c { + case CONTACTOR_CLOSED: + return "contactor closed" + case CONTACTOR_OPEN: + return "contactor open" + default: + panic("ContactorVehicleStatus not implemented") + } +} + +func (s StopRequest) String() string { + switch s { + case NO_REQUEST: + return "no request" + case STOP_REQUEST: + return "stop request" + default: + panic("StopRequest not implemented") + } }