Overview
The general idea of charger is to poll if the charger is plugged in, then request permission from centre console to charge and start charging if allowed. When the battery is full or the charger is unplugged, we stop charging.
Modules:
charger_controller: This module is effectively a wrapper around mcp2515 that manages communication explicitly with the charger. It can be thought of as the ‘driver’ for the charger.
uses mcp2515’s driver to send charging messages
methods:
charger_controller_init
charger_controller_activate
charger_controller_deactivate
charger_contoller_fault_monitor (merged into charger_controller)
registers can rx callbacks to find faults.
in case of a fault, broadcasts it to the rest of the carUpon receiving a message from the charger, checks for fault bits in the status, and sends a CHARGER_FAULT message with the appropriate fault.
charger_control_pilot_monitor
when PWM_READING_REQUEST is raised, it will use PWM to do a reading for max current.
raises a PWM_READING event with the max current readingExposes
uint16_t control_pilot_get_current()
to read the PWM of the control pilot pin. Returns a value in deciamps that is the maximum current the charger can provide.charger_connection_sense
sets up the interrupt for when the charger gets powered from AC.
raises a CHARGER_CONNECTED, CHARGER_DISCONNECTED, event when the interrupt triggers.
permission_resolver
once the charger connects, it sends a permission request to centre console, here’s two cases:
centre console will grant permission
it starts the begin_charge_fsm
centre console won’t grant permission
charger does nothing, and waits for a disconnect and connect.
begin_charge_fsm
it begins PWM reading by raising a PWM_READING_REQUEST
then it will wait for MAX_CURRENT_AVAILABLE
then it will set the state of the control pilot
then it activates charger_controller and charging begins.
it will also handle relay controls
stop_charge_fsm
it will stop the charger_controller
it will set the control pilot pin
it will broadcast a CHARGER_DISCONNECTED CAN message. So centre console can allow for power state transitions.
it will also handle relay controls
battery_voltage_monitor
repeatedly receives CAN_(battery_voltage)
if more than a threshold, begins the stop_charge_fsm
: wraps PWM, also converts PWM value to current based on J1772 standard.
Incoming CAN Messages:
...
Message
...
CHARGING_GRANTED
...
BM
Outgoing CAN Messages:
...
Message
...
REQUEST_TO_CHARGE
...
CHARGER_CONNECTION
...
1: connected 0: disconnected
...
: periodically reads the state of the connection_sense pin to determine whether or not the charger is connected. The charger can be unplugged, plugged in with the button pressed, or plugged in with the button released. You need to press the button to unplug the charger (think of it like the handle on a car door - the door can be open, closed with handle pulled, or closed with handle not pulled).
periodically poll the connection sense (CS) pin via ADC to determine state (defined at 500 ms)
upon connecting or disconnecting, transmit
SYSTEM_CAN_MESSAGE_CHARGER_CONNECTED_STATE
with the appropriate exported enum value, and raise aCHARGER_CHARGE_EVENT_STOP/BEGIN
if it’s disconnected or connected respectively.
begin_sequence: handles the sequence of things that need to happen to begin charging
upon processing a
CHARGER_CHARGE_EVENT_BEGIN
event, transmitSYSTEM_CAN_MESSAGE_REQUEST_TO_CHARGE
.Centre console will get the request. If we’re in the appropriate drive state (parking) centre console will send back
SYSTEM_CAN_MESSAGE_ALLOW_CHARGING
.
Upon receiving a
SYSTEM_CAN_MESSAGE_ALLOW_CHARGING
, begin the init sequence:make sure charger is on (transmit
SYSTEM_CAN_MESSAGE_CHARGER_FAULT
if not)Get a pwm reading from control pilot to deduce max current allowed
set relay state then load switch state
enable charging via control pilot
activate charger via
charger_controller_activate
stop_sequence: handles the sequence of things that need to happen to stop charging
Upon processing a
CHARGER_CHARGE_EVENT_STOP
, the sequence will:deactivate the charger via
charger_controller_deactivate
Turn off the control pilot
Turn off the load switch then the relay
battery_monitor: stops charging when the battery is full.
repeatedly receives SYSTEM_CAN_MESSAGE_BATTERY_AGGREGATE_VC
If voltage is over 135 V (defined threshold) will raise a CHARGER_CHARGE_EVENT_STOP event.
Operation Mode:
The BMS sends operating information (Message 1) to charger at fixed interval of 1s. After receiving the message, the charger will work under the Voltage and Current in Message. If the Message is not received within 5s, it will enter into communication error state and stop charging.
...