Skip to content

Commit 353ccce

Browse files
authored
Merge branch 'main' into can_fix
2 parents 74b4404 + 66ebc55 commit 353ccce

File tree

20 files changed

+336
-18064
lines changed

20 files changed

+336
-18064
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ mbed-os-build/
4646

4747
# The above gitignores cannot apply within ./mbed-os. This MUST be last
4848
!mbed-os/**
49+
busmasterConfigNathaniel.cfx

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
path = BMS/lib-mbed-ltc681x
33
url = https://github.com/formulaslug/lib-mbed-ltc681x.git
44
shallow = true
5+
[submodule "BMS/onewire-mbed-os"]
6+
path = BMS/onewire-mbed-os
7+
url = https://github.com/alwint3r/onewire-mbed-os.git

BMS/.clang-tidy

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# Generated from CLion Inspection settings
21
---
32
Checks: '-*,
43
bugprone-argument-comment,
@@ -61,7 +60,7 @@ cert-msc50-cpp,
6160
cert-msc51-cpp,
6261
cert-str34-c,
6362
cppcoreguidelines-interfaces-global-init,
64-
cppcoreguidelines-narrowing-conversions,
63+
# cppcoreguidelines-narrowing-conversions,
6564
cppcoreguidelines-pro-type-member-init,
6665
cppcoreguidelines-pro-type-static-cast-downcast,
6766
cppcoreguidelines-slicing,
@@ -93,7 +92,7 @@ modernize-replace-random-shuffle,
9392
modernize-return-braced-init-list,
9493
modernize-shrink-to-fit,
9594
modernize-unary-static-assert,
96-
modernize-use-auto,
95+
# modernize-use-auto,
9796
modernize-use-bool-literals,
9897
modernize-use-emplace,
9998
modernize-use-equals-default,
@@ -144,4 +143,4 @@ readability-static-accessed-through-instance,
144143
readability-static-definition-in-anonymous-namespace,
145144
readability-string-compare,
146145
readability-uniqueptr-delete-release,
147-
readability-use-anyofallof'
146+
readability-use-anyofallof'

BMS/CMakeLists.txt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
1111
set(MBED_APP_JSON_PATH mbed_app.json5)
1212
# set(CUSTOM_TARGETS_JSON_PATH custom_targets.json) # If you need a custom target, use this line to specify the custom_targets.json
1313

14-
# If you need any custom upload method configuration for your target, do that here
14+
# Create a CMake Library target for the onewire-mbed-os library.
15+
add_library(onewire-mbed-os STATIC ./onewire-mbed-os/OneWire.cpp)
16+
target_include_directories(onewire-mbed-os PUBLIC ./onewire-mbed-os)
17+
target_link_libraries(onewire-mbed-os mbed-core-flags)
1518

1619
# Configure upload method for ST-LINK
1720
set(UPLOAD_METHOD_DEFAULT STLINK)
@@ -24,7 +27,7 @@ project(BMS)
2427
include(mbed_project_setup)
2528

2629
add_subdirectory(../mbed-os mbed-os)
27-
# Tell CMake to build lib-mbed-ltc681x
30+
2831
add_subdirectory(lib-mbed-ltc681x)
2932

3033
add_executable(BMS src/main.cpp
@@ -39,6 +42,7 @@ add_executable(BMS src/main.cpp
3942
src/LTC6811.h
4043
src/LTC6811.cpp
4144
src/StateOfCharge.cpp
45+
src/DS18B20.cpp
4246
)
43-
target_link_libraries(BMS mbed-os lib-mbed-ltc681x) # Can also link to mbed-baremetal here
44-
mbed_set_post_build(BMS) # Must call this for each target to set up bin file creation, code upload, etc
47+
target_link_libraries(BMS mbed-os lib-mbed-ltc681x onewire-mbed-os)
48+
mbed_set_post_build(BMS)

BMS/onewire-mbed-os

Submodule onewire-mbed-os added at 5f65ff3

BMS/src/BmsConfig.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,11 @@ struct status_msg {
382382
uint16_t glv_voltage;
383383
uint32_t cell_fault_index;
384384
};
385+
386+
struct tray_temps_msg {
387+
uint8_t temp_busbar;
388+
uint8_t temp_pack_fuse;
389+
uint8_t temp_bolted_connection;
390+
uint8_t temp_cowling;
391+
};
392+

BMS/src/Can.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,21 @@ CANMessage ACC_TPDO_POWER(uint16_t packVoltage, uint8_t state_of_charge, int16_t
137137
return CANMessage{kACC_TPDO_POWER, data};
138138
}
139139

140+
CANMessage ACC_TPDO_TRAY_TEMPS(tray_temps_msg* tray_temps_message) {
141+
uint8_t data[8] = {
142+
tray_temps_message->temp_bolted_connection,
143+
tray_temps_message->temp_busbar,
144+
tray_temps_message->temp_pack_fuse,
145+
tray_temps_message->temp_cowling,
146+
0x0
147+
};
148+
149+
return CANMessage{kACC_TPDO_TRAY_TEMPS, data};
150+
}
151+
140152
// FULL CAN send message, sends all the possible can messages for the ACC in one go
141153
// status is precalled for this to work
142-
void canSend(status_msg* status_message, uint16_t packVolt, uint8_t soc, int16_t curr, uint8_t fan_pwm_duty_cycle, uint16_t* allVoltages,
154+
void canSend(status_msg* status_message, tray_temps_msg* tray_temps_message, uint16_t packVolt, uint8_t soc, int16_t curr, uint8_t fan_pwm_duty_cycle, uint16_t* allVoltages,
143155
int8_t* allTemps) {
144156

145157
// status
@@ -165,6 +177,10 @@ void canSend(status_msg* status_message, uint16_t packVolt, uint8_t soc, int16_t
165177
canBus->write(ACC_TPDO_POWER(packVolt, soc, curr, fan_pwm_duty_cycle));
166178
ThisThread::sleep_for(1ms);
167179

180+
//tray temps
181+
canBus->write(ACC_TPDO_TRAY_TEMPS(tray_temps_message));
182+
ThisThread::sleep_for(1ms);
183+
168184
// all the segment volts
169185
canBus->write(ACC_TPDO_SEG0_VOLTS(allVoltages));
170186
ThisThread::sleep_for(1ms);
@@ -188,4 +204,4 @@ void canSend(status_msg* status_message, uint16_t packVolt, uint8_t soc, int16_t
188204
ThisThread::sleep_for(1ms);
189205
canBus->write(ACC_TPDO_SEG4_TEMPS(allTemps));
190206
ThisThread::sleep_for(1ms);
191-
}
207+
}

BMS/src/Can.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// SIDs From Accumulator TODO: DOUBLE CHECK THESE
1313
constexpr uint32_t kACC_TPDO_STATUS = 0x188;
1414
constexpr uint32_t kACC_TPDO_POWER = 0x288;
15+
constexpr uint32_t kACC_TPDO_TRAY_TEMPS = 0x388;
1516

1617
constexpr uint32_t kACC_TPDO_SEG0_TEMPS = 0x291;
1718
constexpr uint32_t kACC_TPDO_SEG1_TEMPS = 0x292;
@@ -66,12 +67,11 @@ CANMessage ACC_TPDO_SEG4_VOLTS(uint16_t *volts);
6667
CANMessage ACC_TPDO_POWER(uint16_t packVoltage, uint8_t state_of_charge, uint8_t fan_pwm_duty_cycle, int16_t current);
6768

6869

69-
70-
void canSend(status_msg *status_message, uint16_t packVolt, uint8_t soc, int16_t curr, uint8_t fan_pwm_duty_cycle, uint16_t* allVoltages,
70+
void canSend(status_msg *status_message, tray_temps_msg* tray_temps_message, uint16_t packVolt, uint8_t soc, int16_t curr, uint8_t fan_pwm_duty_cycle, uint16_t* allVoltages,
7171
int8_t* allTemps);
7272

7373

7474

7575
void writeCAN(string name, CANMessage message);
7676

77-
#endif
77+
#endif

BMS/src/DS18B20.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#include "DS18B20.h"
2+
#include "mbed.h"
3+
4+
void debug_search_for_ds18b20_address(OneWire& bus) {
5+
uint8_t addr[8];
6+
7+
if (!bus.search(addr)) {
8+
printf("No more addresses.\r\n\r\n");
9+
bus.reset_search();
10+
ThisThread::sleep_for(250ms);
11+
return;
12+
}
13+
14+
printf("ROM = ");
15+
for (uint8_t byte : addr) {
16+
printf(" %x", byte);
17+
}
18+
19+
if (OneWire::crc8(addr, 7) != addr[7]) {
20+
printf("CRC is not valid!\r\n\r\n");
21+
return;
22+
}
23+
24+
// the first ROM byte indicates which chip
25+
switch (addr[0]) {
26+
case 0x10:
27+
printf(" Chip = DS18S20 (type_s) = 1\r\n"); // or old DS1820
28+
break;
29+
case 0x28:
30+
printf(" Chip = DS18B20 (type_s) = 0\r\n");
31+
break;
32+
case 0x22:
33+
printf(" Chip = DS1822 (type_s) = 0\r\n");
34+
break;
35+
default:
36+
printf("Device is not a DS18x20 family device.\r\n");
37+
return;
38+
}
39+
}
40+
41+
DS18B20::DS18B20(OneWire& onewire_bus, uint64_t device_address)
42+
: bus(onewire_bus), address(device_address) {}
43+
44+
void DS18B20::start_conversion(bool parasite_power_mode) {
45+
bus.reset();
46+
bus.select((uint8_t*)&address);
47+
bus.write(0x44, parasite_power_mode ? 1 : 0); // start conversion, with parasite power on at the end
48+
}
49+
50+
uint8_t DS18B20::retrieve_conversion(bool type_s_sensor) {
51+
// we might do a ds18b20.depower() here, but the reset will take care of it.
52+
53+
/* uint8_t present = */ bus.reset();
54+
bus.select((uint8_t*)&address);
55+
bus.write(0xBE); // Read Scratchpad
56+
57+
// printf(" Data = %x \r\n\r\n", present);
58+
uint8_t data[12];
59+
for (int i = 0; i < 9; i++) { // we need 9 bytes
60+
data[i] = bus.read();
61+
// printf(" %x", data[i]);
62+
}
63+
// printf("\r\n");
64+
// printf(" CRC= %x \r\n\r\n", OneWire::crc8(data, 8));
65+
66+
// Convert the data to actual temperature
67+
// because the result is a 16 bit signed integer, it should
68+
// be stored to an "int16_t" type, which is always 16 bits
69+
// even when compiled on a 32 bit processor.
70+
int16_t raw = ((data[1] << 8) | data[0]);
71+
if (type_s_sensor) {
72+
raw = raw << 3; // 9 bit resolution default
73+
if (data[7] == 0x10) {
74+
// "count remain" gives full 12 bit resolution
75+
raw = (raw & 0xFFF0) + 12 - data[6];
76+
}
77+
} else {
78+
uint8_t cfg = (data[4] & 0x60);
79+
// at lower res, the low bits are undefined, so let's zero them
80+
if (cfg == 0x00)
81+
raw = raw & ~7; // 9 bit resolution, 93.75 ms
82+
else if (cfg == 0x20)
83+
raw = raw & ~3; // 10 bit res, 187.5 ms
84+
else if (cfg == 0x40)
85+
raw = raw & ~1; // 11 bit res, 375 ms
86+
//// default is 12 bit resolution, 750 ms conversion time
87+
}
88+
uint8_t celsius_x2 = raw / 8;
89+
// float celsius = (float)raw / 16.0;
90+
// fahrenheit = celsius * 1.8 + 32.0;
91+
return celsius_x2;
92+
}

BMS/src/DS18B20.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "mbed.h"
2+
#include "OneWire.h"
3+
4+
// For debugging only: search for onewire device addresses on the bus
5+
// (useful for setup process)
6+
void debug_search_for_ds18b20_address(OneWire& bus);
7+
8+
class DS18B20 {
9+
public:
10+
explicit DS18B20(OneWire& onewire_bus, uint64_t device_address);
11+
12+
// Begin temperature sense process
13+
void start_conversion(bool parasite_power_mode = false);
14+
15+
// returns temperature in Degrees Celcius, multiplied by 2 (eg. 42 ==> 21C)
16+
uint8_t retrieve_conversion(bool type_s_sensor = false);
17+
18+
private:
19+
OneWire& bus;
20+
uint64_t address;
21+
};

0 commit comments

Comments
 (0)