Skip to content

Extend the lispbm function (extension) from external source #75

@mvturnho

Description

@mvturnho

This is actualy not an issue, but more a question.
I have made a port of the vesc_express to the ESP32S3 N8R16 and implemented an ebike assist functionality with pas sensor, brake sensor, assist levels etc.

My code is in the components directory so I have minimal changes in the vesc_express sources. I need to extend the can_comm for my display packets, so maybe you have a tip on how to process custom can frames without changing the base code.

And how can I extend the lispbm functionality like fetching the brake state or assist level etc.

My main goal is to use the base vesc_express code where my code is an addon to add ebike assist functionality. The assist code controls the motor and display over can-bus.

When I have sorted out the above issues I wil publish the assist code on github.

Best regards,

Michiel

Assist-code

To use the assist functionality from the lispBM scripts I would like to add these as extensions for functions and variables. Now I use this

lbm_add_extension(...)

it does compile but does not work from my script. My first use case is to controll a fastled strip with the brakesensor and light settings from the display.

void ebike_assist_init(void) {
	
	state = ASSIST_IDLE;
	pid_initialized = 0;

	ebike_state.current_ramp_rate = backup.config.ebike_current_ramprate;
	ebike_state.assist_can_id = backup.config.ebike_motor_can_id;

	// Parse power levels from config string
	parse_assist_levels(backup.config.ebike_assist_power_levels);

	brake_init(BRAKE_PIN);
	pas_init(PAS_PIN);
	light_init(LIGHT_PIN, BRAKE_LIGHT_PIN);

	xTaskCreatePinnedToCore(
		ebike_update_task, "ebike_update", 4096, NULL, 5, &update_task_handle,
		tskNO_AFFINITY
	);

	assist_can_start_task(backup.config.controller_id);

	ebike_terminal_init();

	lbm_add_extension("assist_get_brake_state", ext_assist_get_brake_state);

	initialized = true;
}

static lbm_value ext_assist_get_brake_state(lbm_value* args, lbm_uint argn) {
	(void)args;
	(void)argn;
	return brake_is_active() ? 1 : 0;
}

Can processing of custom packets

Here I extended the vesc code to process my assist packets from display status. I would like some advice on how to do this without having to change the vesc code.

static void process_task(void *arg) {
	for (;;) {
		xSemaphoreTake(proc_sem, 10 / portTICK_PERIOD_MS);

		while (rx_read != rx_write) {
			twai_message_t *msg = &rx_buf[rx_read];
			rx_read++;
			if (rx_read >= RXBUF_LEN) {
				rx_read = 0;
			}

			lispif_process_can(msg->identifier, msg->data, msg->data_length_code, msg->extd);

			if (use_vesc_decoder) {
				if (!bms_process_can_frame(msg->identifier, msg->data, msg->data_length_code, msg->extd)) {
					if (msg->extd) {
						decode_msg(msg->identifier, msg->data, msg->data_length_code, false);
						assist_can_decode_msg(msg->identifier, msg->data, msg->data_length_code, false);
					}
				}
			}
		}
	}

	vTaskDelete(NULL);
}
void assist_can_decode_msg(uint32_t eid, uint8_t* data8, int len, bool is_replaced) {
    // This function can be used to decode incoming CAN messages if needed in the future.
    // Currently, assist_can_multicast is only sending data and not processing incoming messages.
    int32_t ind = 0;
    uint8_t crc_low;
    uint8_t crc_high;
    uint8_t commands_send;

    uint8_t id = eid & 0xFF;
    ASSIST_CAN_PACKET_ID cmd = eid >> 8;
    switch (cmd) {
    case CAN_PACKET_DISPLAY_STATUS: {
        // | Byte | Bits | Signaal | Omschrijving |
        // | ---- | ---- | ---- | ---- |
        // | 0 | 0–3 | assist_level | Gekozen assist level(lookup input) |
        // |     | 4–7 | reserved | |
        // | 1 | 0 | light_on | Alleen relevant indien assist licht stuurt |
        // |     | 1–7 | reserved | |
        // | 2 | 0–7 | alive_counter_LSB | 16 - bit heartbeat |
        // | 3 | 0–7 | alive_counter_MSB | 16 - bit heartbeat |
        // | 4–7 | – | reserved | |
        if (len >= 8) {
            display_state.assist_level = data8[0] & 0x0F;
            display_state.light_on = (data8[1] & 0x01) != 0;
            display_state.alive_counter = (uint16_t)data8[2] | ((uint16_t)data8[3] << 8);
        }
    } break;
......

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions