Skip to content

Commit 94eec85

Browse files
authored
Merge pull request #22 from formulaslug/ETC
ETC Merge into main
2 parents 36bff0f + ae848c8 commit 94eec85

File tree

6 files changed

+50
-30
lines changed

6 files changed

+50
-30
lines changed

ETC/main.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ void do_can_processing() {
3535
// Wait for any event flag to be set (defined in the can wrapper class)
3636
uint32_t triggered_flags =
3737
global_events.wait_any(can_handle->THROTTLE_FLAG | can_handle->STATE_FLAG |
38-
can_handle->SYNC_FLAG | can_handle->RX_FLAG);
39-
38+
can_handle->SYNC_FLAG | can_handle->RX_FLAG | can_handle->LIMITS_FLAG);
39+
can_handle->processCANRx();
4040
/* Check for every event, process and then clear the corresponding flag */
4141
if (triggered_flags & can_handle->THROTTLE_FLAG) {
4242
// DPRINT("Flag Trigger: Throttle\n");
@@ -53,6 +53,10 @@ void do_can_processing() {
5353
can_handle->sendSync();
5454
global_events.clear(can_handle->SYNC_FLAG);
5555
}
56+
if (triggered_flags & can_handle->LIMITS_FLAG) {
57+
can_handle->sendCurrentLimits();
58+
global_events.clear(can_handle->LIMITS_FLAG);
59+
}
5660
if (triggered_flags & can_handle->RX_FLAG) {
5761
// DPRINT("Flag: CAN RX\n");
5862
can_handle->processCANRx();

ETC/src/can_wrapper.cpp

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ CANWrapper::CANWrapper(ETCController& etcController, EventFlags& events)
99
etc(etcController)
1010
{
1111
this->bus = new CAN(CANWrapper::CAN_RX_PIN, CANWrapper::CAN_TX_PIN, CANWrapper::CAN_FREQUENCY);
12+
this->bus->filter(0x188, 0xFFFF, CANAny);
1213

1314
// Start regular ISR routine for sending
1415
this->throttleTicker.attach(callback([this]() {
@@ -23,6 +24,10 @@ CANWrapper::CANWrapper(ETCController& etcController, EventFlags& events)
2324
this->globalEvents.set(CANWrapper::STATE_FLAG);
2425
}), 100ms);
2526

27+
this->limitsTicker.attach(callback([this]() {
28+
this->globalEvents.set(CANWrapper::LIMITS_FLAG);
29+
}), 100ms);
30+
2631
// Set up CAN receive ISR
2732
this->bus->attach(callback([this]() {
2833
this->globalEvents.set(CANWrapper::RX_FLAG);
@@ -50,6 +55,7 @@ void CANWrapper::sendThrottle() {
5055
throttleMessage.data[7] = 0x00;
5156

5257
this->bus->write(throttleMessage);
58+
ThisThread::sleep_for(1ms);
5359
}
5460

5561

@@ -58,6 +64,7 @@ void CANWrapper::sendSync() {
5864
CANMessage syncMessage(0x80, data, 0);
5965

6066
this->bus->write(syncMessage);
67+
ThisThread::sleep_for(1ms);
6168
}
6269

6370

@@ -76,7 +83,7 @@ void CANWrapper::sendState() {
7683
stateMessage.data[4] = static_cast<uint8_t>(static_cast<int16_t>(state.brakes_read * 1000) & 0xFF);
7784
stateMessage.data[5] = static_cast<uint8_t>(static_cast<int16_t>(state.brakes_read * 1000) >> 8);
7885

79-
stateMessage.data[6] = static_cast<uint8_t>(state.pedal_travel * 255);
86+
stateMessage.data[6] = static_cast<uint8_t>(state.pedal_travel * 100);
8087

8188
stateMessage.data[7] =
8289
(state.cockpit) |
@@ -86,21 +93,9 @@ void CANWrapper::sendState() {
8693
(state.motor_enabled << 4) |
8794
(this->etc.hasImplausibility() << 5) |
8895
(state.ts_ready << 6);
89-
90-
91-
stateMessage.data[0] =
92-
(0x01 & state.ts_ready) |
93-
((0x01 & state.motor_enabled) << 1) |
94-
((0x01 & state.cockpit << 4));
95-
stateMessage.data[1] = static_cast<int8_t>(state.brakes_read * 100);
96-
stateMessage.data[2] = static_cast<int8_t>(state.he1_travel * 100);
97-
stateMessage.data[3] = static_cast<int8_t>(state.he2_travel * 100);
98-
stateMessage.data[4] = static_cast<int8_t>(state.pedal_travel * 100);
99-
stateMessage.data[5] = 0x00;
100-
stateMessage.data[6] = 0x00;
101-
stateMessage.data[7] = 0x00;
10296

10397
this->bus->write(stateMessage);
98+
ThisThread::sleep_for(1ms);
10499
}
105100

106101

@@ -113,21 +108,31 @@ void CANWrapper::sendCurrentLimits() {
113108
currentMessage.data[1] = 0x00;
114109

115110
// Constant discharge current = 400A (split into little endian order)
116-
currentMessage.data[2] = 0b10010000;
117-
currentMessage.data[3] = 0b00000001;
111+
currentMessage.data[2] = static_cast<uint8_t>(400);
112+
currentMessage.data[3] = static_cast<uint8_t>(400 >> 8);
118113

119114
currentMessage.data[4] = 0x00;
120115
currentMessage.data[5] = 0x00;
121116
currentMessage.data[6] = 0x00;
122117
currentMessage.data[7] = 0x00;
123118

124-
this->bus->write(currentMessage);
119+
this->bus->write(currentMessage);
120+
ThisThread::sleep_for(1ms);
125121
}
126122

127123

128124
void CANWrapper::processCANRx() {
125+
// printf("rxerr: %d\n", this->bus->rderror());
126+
// printf("txerr: %d\n", this->bus->tderror());
127+
// printf("rx\n");
129128
CANMessage rx;
130-
if (this->bus->read(rx)) {
131-
/** TODO: process the received message... */
129+
while (this->bus->read(rx)) {
130+
switch (rx.id) {
131+
case 0x188: // ACC_TPDO_STATUS
132+
ETCState state = this->etc.getState();
133+
state.ts_ready = rx.data[0] & 0x08;
134+
this->etc.updateStateFromCAN(state);
135+
break;
136+
}
132137
}
133138
}

ETC/src/can_wrapper.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class CANWrapper {
2727
Ticker throttleTicker;
2828
/** Ticker to send regular sync messages. */
2929
Ticker syncTicker;
30+
/** Ticker to send regular current limits messages. */
31+
Ticker limitsTicker;
3032
/** Ticker to send regular ETC state summary messages. */
3133
Ticker stateTicker;
3234

@@ -40,12 +42,18 @@ class CANWrapper {
4042

4143
/** The flag bit indicating a throttle message needs to be sent. */
4244
static constexpr int32_t THROTTLE_FLAG = (1UL << 0);
45+
4346
/** The flag bit indicating a sync message needs to be sent. */
4447
static constexpr int32_t SYNC_FLAG = (1UL << 1);
48+
4549
/** The flag bit indicating a state message needs to be sent. */
4650
static constexpr int32_t STATE_FLAG = (1UL << 2);
51+
52+
/** The flag bit indicating a current limits flag needs to be sent. */
53+
static constexpr int32_t LIMITS_FLAG = (1UL << 3);
54+
4755
/** The flag bit indicating received messages need to be processed. */
48-
static constexpr int32_t RX_FLAG = (1UL << 3);
56+
static constexpr int32_t RX_FLAG = (1UL << 4);
4957

5058

5159
/**

ETC/src/etc_controller.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ bool ETCController::isCockpitSwitchSet() const {
222222

223223

224224
void ETCController::switchReverseMotor() {
225-
this->state.motor_forward = false;
225+
// this->state.motor_forward = false;
226226
}
227227

228228

ETC/src/etc_controller.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,23 +88,24 @@ class ETCController {
8888
static constexpr float MAX_VOLTAGE = 3.3f;
8989

9090
/** The percentage tolerance for the brake pedal to be considered pressed. */
91-
static constexpr float BRAKE_TOLERANCE = 0.382f;
91+
// static constexpr float BRAKE_TOLERANCE = 0.382f;
92+
static constexpr float BRAKE_TOLERANCE = 0.0f;
9293

9394
/** The voltage divider slope for the hall-effect 1 sensor. */
94-
static constexpr float HE1_SCALE = 330.0f / 480.0f;
95+
static constexpr float HE1_SCALE = (330.0f / 480.0f);
9596
/** The voltage for HE1 corresponding to 0% pedal travel. */
96-
static constexpr float HE1_LOW_VOLTAGE = 2.4000000f;
97+
static constexpr float HE1_LOW_VOLTAGE = 1.824f;
9798
/** The voltage for HE1 corresponding to 100% pedal travel. */
98-
static constexpr float HE1_HIGH_VOLTAGE = 2.8000000f;
99+
static constexpr float HE1_HIGH_VOLTAGE = 2.806f;
99100
/** The difference between the maximum and minimum voltages (100% and 0% travel) for HE1. */
100101
static constexpr float HE1_RANGE =
101102
ETCController::HE1_HIGH_VOLTAGE - ETCController::HE1_LOW_VOLTAGE;
102103
/** The voltage divider slope for the hall-effect 2 sensor. */
103-
static constexpr float HE2_SCALE = 1.0f / 2.0f;
104+
static constexpr float HE2_SCALE = (1.0f / 2.0f);
104105
/** The voltage for HE2 corresponding to 0% pedal travel. */
105-
static constexpr float HE2_LOW_VOLTAGE = 1.900000f;
106+
static constexpr float HE2_LOW_VOLTAGE = 1.449f;
106107
/** The voltage for HE2 corresponding to 100% pedal travel. */
107-
static constexpr float HE2_HIGH_VOLTAGE = 2.2290114f;
108+
static constexpr float HE2_HIGH_VOLTAGE = 2.206f;
108109
/** The difference between the maximum and minimum voltages (100% and 0% travel) for HE2. */
109110
static constexpr float HE2_RANGE =
110111
ETCController::HE2_HIGH_VOLTAGE - ETCController::HE2_LOW_VOLTAGE;

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ Projects in this repo require different build toolchains. To make sure that you
66
__FOR ALL TEAM MEMBERS:__ Create a fork on your personal github and start pull requests when you are ready to submit your work.
77

88

9+
## Requirements
10+
Python 3.10 or below.

0 commit comments

Comments
 (0)