Compare commits

...
4 Commits
6 changed files with 365 additions and 64 deletions

No files matched your search

+3 -1
View File
@@ -56,7 +56,9 @@ jobs:
run: |
for f in ${{ steps.changed.outputs.all_changed_files }}; do
# Zephyr RTOS platforms need the esphomeplatformzephyr fork, upstream doesn't know them yet
if grep -qE '^(nrf|stm32|nxp|renesas|silabs):' "$f"; then echo "Skipping $f (zephyr fork)"; continue; fi
# (the platform key may also come from an !include'd package, e.g. common/xiao_stm32c5_canopen.yaml)
files="$f $(grep -oE '!include [^ ]+' "$f" | cut -d' ' -f2)"
if grep -qsE '^(nrf|stm32|nxp|renesas|silabs):' $files; then echo "Skipping $f (zephyr fork)"; continue; fi
echo "Validating $f..."
esphome config "$f"
done
+61 -2
View File
@@ -28,6 +28,8 @@ logger:
logs:
# canbus logs every frame at DEBUG; over USB CDC that stalls the loop and the RX queue overflows
canbus: INFO
# the same for pitch/roll every 200 ms
sensor: INFO
i2c:
# pins come from the board DTS (i2c2: PB3/PB4), STM32C5 takes no sda:/scl:
@@ -39,22 +41,69 @@ motion:
id: imu
i2c_id: i2c_imu
address: 0x6A
update_interval: 5s
# a live inclinometer (maptool's dashboard / Terrain view) needs 100-200 ms
update_interval: 200ms
sensor:
- platform: lsm6ds
id: imu_temp
name: IMU Temperature
# Levelled by the node itself (the mounting error belongs to the sensor, every reader of the bus gets it
# corrected): raw = state + offset, so levelling again adds to the offsets
- platform: motion
motion_id: imu
id: pitch
type: pitch
name: Pitch
filters:
- lambda: return x - id(pitch_offset).state;
- platform: motion
motion_id: imu
id: roll
type: roll
name: Roll
filters:
- lambda: return x - id(roll_offset).state;
number:
- platform: template
id: pitch_offset
name: Pitch Offset
entity_category: config
unit_of_measurement: "°"
optimistic: true
restore_value: true
initial_value: 0
min_value: -45
max_value: 45
step: 0.01
- platform: template
id: roll_offset
name: Roll Offset
entity_category: config
unit_of_measurement: "°"
optimistic: true
restore_value: true
initial_value: 0
min_value: -45
max_value: 45
step: 0.01
# canopen_entities has no button: a switch that turns itself back off. On uneven ground: level, turn the car
# 180° on the same spot, level again and take the mean (maptool writes the offsets for that)
switch:
- platform: template
id: level_imu
name: Level IMU
entity_category: config
lambda: return false;
turn_on_action:
- lambda: |-
if (std::isnan(id(pitch).state) || std::isnan(id(roll).state))
return;
id(pitch_offset).make_call().set_value(id(pitch_offset).state + id(pitch).state).perform();
id(roll_offset).make_call().set_value(id(roll_offset).state + id(roll).state).perform();
ESP_LOGI("level", "levelled: pitch offset %.2f, roll offset %.2f", id(pitch_offset).state, id(roll_offset).state);
output:
- platform: gpio
@@ -80,8 +129,9 @@ canbus:
# an SDO block (16 frames) arrives back to back and must fit the RX queue (default 8)
rx_buffer_size: 32
# a list, so a node file can add a second local node (e.g. a PKP keypad emulation)
canopen:
id: can_gate
- id: can_gate
canbus_id: can_bus
node_id: ${node_id}
heartbeat_interval: 5s
@@ -111,11 +161,20 @@ canopen_entities:
size: 2
min_value: -40
max_value: 125
# levelling (maptool /can.html): Level IMU = SDO 0x2022:1 <- 1; offsets REAL32, state 0x2101 / 0x2111,
# command 0x2102:1 / 0x2112:1. 16/17: the nodes' own entities are 6..15
- id: level_imu
index: 2
- id: pitch_offset
index: 16
- id: roll_offset
index: 17
remote_logs:
level: INFO
ota:
- platform: canopen_entities
canopen_id: can_gate
# the XIAO's board layout has slot0 one sector larger than slot1 (swap-move); with the fork's default
# (offset) MCUboot finds the slots incompatible and never installs the update
swap_method: move
+29 -53
View File
@@ -24,9 +24,8 @@ esp32:
type: esp-idf
external_components:
- source: github://mrk-its/esphome-canopen@dev
refresh: 1h
- source: github://mrk-its/esphome-canbus-udp-multicast@dev
- source: github://mrk-its/esphome-canopen@canopen_entities
components: [canopen_entities]
refresh: 1h
packages:
@@ -44,52 +43,22 @@ canopen:
id: can_open
canbus_id: can_bus
node_id: 1
entities:
- index: 1
id: light1_state
rpdo:
- node_id: 2
tpdo: 0
offset: 0
cmd: 0
- index: 2
id: light1_brightness
size: 1
min_value: 0.0
max_value: 1.0
rpdo:
- node_id: 2
tpdo: 0
offset: 1
cmd: 0
- index: 3
id: light2_state
rpdo:
- node_id: 2
tpdo: 0
offset: 3
cmd: 0
- index: 4
id: light2_brightness
size: 1
min_value: 0.0
max_value: 1.0
rpdo:
- node_id: 2
tpdo: 0
offset: 4
cmd: 0
canopen_entities:
remote_nodes:
node2: mcu-can-node-2.yaml
binary_sensor:
- platform: template
- platform: canopen_entities
id: light1_state
name: Light 1 State
- platform: template
node: node2
entity: light1
- platform: canopen_entities
id: light2_state
name: Light 2 State
node: node2
entity: light2
- platform: template
id: light1_state_cmd
@@ -105,26 +74,33 @@ binary_sensor:
pullup: true
on_press:
then:
- lambda: |-
bool state = id(light1_state).state || id(light2_state).state;
// send command to entity #1 (light) on node #2
id(can_open).send_entity_cmd(2, 1, !state);
// send command to entity #2 (light2) on node #2
id(can_open).send_entity_cmd(2, 2, !state);
# both lights on node 2 off if either is on, otherwise both on
- if:
condition:
lambda: return id(light1_state).state || id(light2_state).state;
then:
- canopen_entities.light.turn_off: {node: node2, entity: light1}
- canopen_entities.light.turn_off: {node: node2, entity: light2}
else:
- canopen_entities.light.turn_on: {node: node2, entity: light1}
- canopen_entities.light.turn_on: {node: node2, entity: light2}
sensor:
- platform: template
- platform: canopen_entities
id: light1_brightness
name: Light 1 Brightness
- platform: template
node: node2
entity: light1
state: brightness
- platform: canopen_entities
id: light2_brightness
name: Light 2 Brightness
node: node2
entity: light2
state: brightness
switch:
- platform: gpio
name: LED
id: blue_led
internal: true
pin: 2
+4 -3
View File
@@ -28,9 +28,8 @@ esp32:
type: esp-idf
external_components:
- source: github://mrk-its/esphome-canopen@dev
refresh: 1h
- source: github://mrk-its/esphome-canbus-udp-multicast@dev
- source: github://mrk-its/esphome-canopen@canopen_entities
components: [canopen_entities]
refresh: 1h
packages:
@@ -48,6 +47,8 @@ canopen:
id: can_open
canbus_id: can_bus
node_id: 2
canopen_entities:
entities:
- index: 1
id: light1
+6 -5
View File
@@ -1,11 +1,11 @@
---
# Hardware: M5StickC Plus + Hat CardKB + Hat Mini JoyC on the hat header, Unit CAN on Grove
# Hardware: M5StickC Plus + Hat CardKB + Hat Mini JoyC on the hat header, TJA1050 CAN module on Grove
# CANopen keypad + joystick (node 4), canopen from ESPHome dev + canopen_entities
# Pinout:
# GPIO0 hat SDA -> i2c_hat (i2c sda)
# GPIO26 hat SCL -> i2c_hat (i2c scl)
# GPIO32 Grove -> can_bus (canbus esp32_can tx_pin), swap with rx if the node stays silent
# GPIO33 Grove -> can_bus (canbus esp32_can rx_pin)
# GPIO32 Grove -> can_bus (canbus esp32_can rx_pin), TJA1050 module RX
# GPIO33 Grove -> can_bus (canbus esp32_can tx_pin), TJA1050 module TX
# GPIO21 internal SDA -> i2c_axp (i2c sda)
# GPIO22 internal SCL -> i2c_axp (i2c scl)
# GPIO10 red LED (active low) -> LED (light binary)
@@ -110,10 +110,11 @@ m5stack_mini_joyc:
canbus:
- platform: esp32_can
id: can_bus
tx_pin: 32
rx_pin: 33
tx_pin: 33
rx_pin: 32
can_id: 4
bit_rate: 500kbps
mode: LISTENONLY # TEMP: the node does not see the bus on RX (GPIO32), keep it off the bus
canopen:
- id: can_open
+262
View File
@@ -2,12 +2,274 @@
# Hardware: Seeed XIAO STM32C5 #1, CANopen node 0x20 (see common/xiao_stm32c5_canopen.yaml); its LED is light
# entity 1 (0x2011:1 / 0x2012:1), the server for mcu-stm32-xiao-c5-sdo-client.yaml (esphome#22410 test)
# USB serial: 3838333031365105001F005D (TinyUF2: 5D001F000551363130333838)
# Keypad prototype (Blink Marine PKP compatible) until the MCP23017 board exists, M5 units on Grove:
# D4/D5 (i2c1, PB7 SDA, PB6 SCL) -> i2c_grove; two i2c buses: esphomeplatformzephyr/esphome#247, merged in framework-zephyr
# I2C:
# 0x41 enc (m5stack_8encoder), Unit 8Encoder
# 0x47 byte_button (m5stack_byte), Unit ByteButton -> PKP 2400SI (2x4) emulation, node 0x14
substitutions:
node_id: "0x20"
esphome:
name: mcu-stm32-xiao-c5
friendly_name: XIAO STM32C5
on_boot:
- lambda: |-
// PKP RPDOs: 0x200 LED on, 0x300 LED blink (bytes 0-2: R, G, B masks of keys 1-8), 0x400 brightness 0-3F
static canopen::FrameListener pkp_rx{0x014, 0x0FF, [](void *, uint32_t can_id, const uint8_t *d, uint8_t len) {
if (id(pkp).get_nmt_state() != canopen::NMTState::NMT_STATE_OPERATIONAL || len == 0)
return;
uint32_t bits = d[0] | (len > 1 ? d[1] << 8 : 0) | (len > 2 ? d[2] << 16 : 0);
if (can_id == 0x214)
id(pkp_led_on) = bits;
else if (can_id == 0x314)
id(pkp_led_blink) = bits;
else if (can_id == 0x414)
id(pkp_brightness) = d[0] > 0x3F ? 0x3F : d[0];
}, nullptr};
id(pkp).add_frame_listener(pkp_rx);
packages:
node: !include common/xiao_stm32c5_canopen.yaml
external_components:
- source: github://randybb/esphome-components
components: [m5stack_byte, m5stack_8encoder]
refresh: 1h
i2c:
- id: i2c_grove
dts_node_override: i2c1
# Blink Marine PKP 2400SI emulation (docs: home-gitops canopen-nodes.adoc): like the real one, not active until
# NMT Start (000#0114); 0x14, because the AtomS3 emulates a PKP 2600SI at 0x15
canopen:
- id: pkp
canbus_id: can_bus
node_id: 0x14
nmt_startup: pre_operational
device_name: PKP2400SI
m5stack_byte:
- id: byte_button
i2c_id: i2c_grove
address: 0x47
# encoders: position 0..8 (min/max here) or free turning (bit set in Encoder Relative Modes, for the map and menus);
# both go to CAN as one byte: the position, or the counter mod 256 (the consumer takes the difference)
m5stack_8encoder:
- id: enc
i2c_id: i2c_grove
encoders:
- id: encoder_1
min_value: 0
max_value: 8
filters: &enc_filters
- lambda: return (int32_t) x & 0xFF;
- id: encoder_2
min_value: 0
max_value: 8
filters: *enc_filters
- id: encoder_3
min_value: 0
max_value: 8
filters: *enc_filters
- id: encoder_4
min_value: 0
max_value: 8
filters: *enc_filters
- id: encoder_5
min_value: 0
max_value: 8
filters: *enc_filters
- id: encoder_6
min_value: 0
max_value: 8
filters: *enc_filters
- id: encoder_7
min_value: 0
max_value: 8
filters: *enc_filters
- id: encoder_8
min_value: 0
max_value: 8
filters: *enc_filters
buttons:
- id: encoder_button_1
- id: encoder_button_2
- id: encoder_button_3
- id: encoder_button_4
- id: encoder_button_5
- id: encoder_button_6
- id: encoder_button_7
- id: encoder_button_8
switch:
id: encoder_switch
number:
# 8Encoder LED colours 0xRRGGBB (1-8 at the encoders, 9 at the switch), set over CAN (SDO), kept over reboots
- &led_color
platform: template
id: led_color_1
name: LED 1 Color
entity_category: config
optimistic: true
restore_value: true
initial_value: 0
min_value: 0
max_value: 0xFFFFFF
step: 1
- {<<: *led_color, id: led_color_2, name: LED 2 Color}
- {<<: *led_color, id: led_color_3, name: LED 3 Color}
- {<<: *led_color, id: led_color_4, name: LED 4 Color}
- {<<: *led_color, id: led_color_5, name: LED 5 Color}
- {<<: *led_color, id: led_color_6, name: LED 6 Color}
- {<<: *led_color, id: led_color_7, name: LED 7 Color}
- {<<: *led_color, id: led_color_8, name: LED 8 Color}
- {<<: *led_color, id: led_color_9, name: LED 9 Color}
# bit i = encoder i turns freely (navigation), else a position 0..8; set over CAN by the consumer (SDO)
- platform: template
id: encoder_relative_modes
name: Encoder Relative Modes
entity_category: config
optimistic: true
restore_value: true
initial_value: 0
min_value: 0
max_value: 255
step: 1
on_value:
- lambda: |-
for (int i = 0; i < 8; i++) {
if ((int) x >> i & 1)
id(enc).set_range(i, INT32_MIN, INT32_MAX);
else
id(enc).set_range(i, 0, 8);
}
globals:
- id: pkp_keys_sent
type: uint8_t
initial_value: '0'
- id: pkp_led_on
type: uint32_t
initial_value: '0'
- id: pkp_led_blink
type: uint32_t
initial_value: '0'
- id: pkp_brightness
type: uint8_t
initial_value: '0x3F'
interval:
# keys TPDO 0x194 (byte 0: keys 1-8) on every change, as the real one (event-driven)
# TODO on arrival: ByteButton bit polarity is not documented; here a pressed key reads 0 (drop the ~ if not)
- interval: 20ms
then:
- lambda: |-
uint8_t keys = ~id(byte_button).get_state();
if (keys == id(pkp_keys_sent) || id(pkp).get_nmt_state() != canopen::NMTState::NMT_STATE_OPERATIONAL)
return;
if (id(pkp).send_frame(0x194, &keys, 1))
id(pkp_keys_sent) = keys;
# LEDs: the on colour; with blink, alternating with the blink colour every 500 ms (off if not on)
- interval: 500ms
then:
- lambda: |-
static bool phase = false;
phase = !phase;
uint8_t lvl = 255 * id(pkp_brightness) / 0x3F;
uint8_t rgb[8 * 3];
for (int i = 0; i < 8; i++) {
auto col = [i](uint32_t b) { return (b >> i & 1) | (b >> (i + 8) & 1) << 1 | (b >> (i + 16) & 1) << 2; };
int on = col(id(pkp_led_on)), bl = col(id(pkp_led_blink));
int c = phase && bl ? bl : (bl && !on ? 0 : on);
rgb[i * 3] = c & 1 ? lvl : 0;
rgb[i * 3 + 1] = c & 2 ? lvl : 0;
rgb[i * 3 + 2] = c & 4 ? lvl : 0;
}
id(byte_button).set_leds(rgb, 8);
# 8Encoder LEDs: the colours from led_color_1..9 (set remotely); turning an encoder shows its position 0..8 on the
# 8 encoder LEDs for ENC_SHOW_MS (bar on dark, none in free turning), its own LED blinks white, then the colours
# come back; the switch LED (9th) is green while the switch is on. Written only when something changed.
- interval: 50ms
then:
- lambda: |-
static const uint32_t ENC_SHOW_MS = 3000;
static const uint8_t BAR[3] = {0, 64, 255}, ACTIVE[3] = {255, 255, 255}, SWITCH_ON[3] = {0, 255, 0};
sensor::Sensor *encs[8] = {id(encoder_1), id(encoder_2), id(encoder_3), id(encoder_4),
id(encoder_5), id(encoder_6), id(encoder_7), id(encoder_8)};
number::Number *colors[9] = {id(led_color_1), id(led_color_2), id(led_color_3), id(led_color_4), id(led_color_5),
id(led_color_6), id(led_color_7), id(led_color_8), id(led_color_9)};
static float last[8] = {NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN};
static int active = -1;
static uint32_t changed_at = 0;
for (int i = 0; i < 8; i++) {
float v = encs[i]->state;
if (std::isnan(v) || v == last[i])
continue;
if (!std::isnan(last[i])) { // not the first read after boot
active = i;
changed_at = millis();
}
last[i] = v;
}
uint8_t rgb[9 * 3];
for (int i = 0; i < 9; i++) {
uint32_t c = std::isnan(colors[i]->state) ? 0 : (uint32_t) colors[i]->state;
rgb[i * 3] = c >> 16;
rgb[i * 3 + 1] = c >> 8;
rgb[i * 3 + 2] = c;
}
if (active >= 0 && millis() - changed_at < ENC_SHOW_MS) {
// free turning: only its own LED
int pos = (int) id(encoder_relative_modes).state >> active & 1 ? 0 : encs[active]->state;
memset(rgb, 0, 8 * 3);
for (int led = 0; led < 8 && led < pos; led++)
memcpy(&rgb[led * 3], BAR, 3);
if (millis() / 250 & 1)
memcpy(&rgb[active * 3], ACTIVE, 3);
}
if (id(encoder_switch).state)
memcpy(&rgb[8 * 3], SWITCH_ON, 3);
static uint8_t sent[9 * 3];
static bool written = false;
if (written && memcmp(rgb, sent, sizeof(rgb)) == 0)
return; // no I2C traffic
if (id(enc).set_leds(rgb, 9)) {
memcpy(sent, rgb, sizeof(rgb));
written = true;
}
# 8Encoder as entities of node 0x20 (the package uses 1-5, 16, 17); LED colours 50-58: command 0x2322:1 + 0x10 per LED (REAL32)
# encoders: one byte each, the position 0..8 (as the PKP3500-MT with its default TOP position 8) or the counter mod 256
canopen_entities:
entities:
- {id: encoder_switch, index: 6, tpdo: 1}
- {id: encoder_relative_modes, index: 7}
- {id: led_color_1, index: 50}
- {id: led_color_2, index: 51}
- {id: led_color_3, index: 52}
- {id: led_color_4, index: 53}
- {id: led_color_5, index: 54}
- {id: led_color_6, index: 55}
- {id: led_color_7, index: 56}
- {id: led_color_8, index: 57}
- {id: led_color_9, index: 58}
- {id: encoder_button_1, index: 30, tpdo: 3}
- {id: encoder_button_2, index: 31, tpdo: 3}
- {id: encoder_button_3, index: 32, tpdo: 3}
- {id: encoder_button_4, index: 33, tpdo: 3}
- {id: encoder_button_5, index: 34, tpdo: 3}
- {id: encoder_button_6, index: 35, tpdo: 3}
- {id: encoder_button_7, index: 36, tpdo: 3}
- {id: encoder_button_8, index: 37, tpdo: 3}
- {id: encoder_1, index: 40, tpdo: 4, size: 1}
- {id: encoder_2, index: 41, tpdo: 4, size: 1}
- {id: encoder_3, index: 42, tpdo: 4, size: 1}
- {id: encoder_4, index: 43, tpdo: 4, size: 1}
- {id: encoder_5, index: 44, tpdo: 4, size: 1}
- {id: encoder_6, index: 45, tpdo: 4, size: 1}
- {id: encoder_7, index: 46, tpdo: 4, size: 1}
- {id: encoder_8, index: 47, tpdo: 4, size: 1}