-
Notifications
You must be signed in to change notification settings - Fork 1
battery monitor tx message #209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
98e5101
ff4607c
ade072d
0dfd2d0
239458f
bc81fca
835eb8e
b1b6906
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| 'steer', | ||
| 'sup', | ||
| 'throttle', | ||
| 'batt', | ||
| ] | ||
| ] | ||
| ) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # ruff: noqa: F821 | ||
|
|
||
| Import('env') | ||
| Import('envs') | ||
|
|
||
|
|
||
| firmware, flash = env.SConscript( | ||
| 'src/SConscript.py', | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where is the |
||
| exports={'env': envs['esp32s3']}, | ||
| ) | ||
|
|
||
| component, name = env.Component(firmware, env.File('component.toml')) | ||
| env.ComponentSubtarget(name, 'flash', flash) | ||
|
|
||
|
|
||
| Return('component') | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| [metadata] | ||
| name = 'batt' | ||
| description = 'battery-monitor' |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,39 @@ | ||||||
| # ruff: noqa: F821 | ||||||
|
|
||||||
| Import('env') | ||||||
|
|
||||||
| node = 'BATT' | ||||||
|
|
||||||
| opencan = env.OpenCan( | ||||||
| network=env['CAN']['NETWORK'], | ||||||
| node=node, | ||||||
| ) | ||||||
|
|
||||||
| source = [ | ||||||
| env.StaticObject( | ||||||
| src, | ||||||
| CPPDEFINES=[ | ||||||
| ('EMBER_NODE_IDENTITY', node), | ||||||
| '$CPPDEFINES', | ||||||
| ], | ||||||
| CPPPATH=[ | ||||||
| env.Dir(opencan[0].dir.name), | ||||||
| '$CPPPATH', | ||||||
| ], | ||||||
| ) | ||||||
| for src in [ | ||||||
| 'batt.c', # Ensure this file exists in your source directory | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| *env['LIBRARIES']['firmware-base'], | ||||||
| ] | ||||||
| ] | ||||||
|
|
||||||
| # Correct the typo in the variable name | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why didn't you just fix it? |
||||||
| source += opencan | ||||||
| source += env['LIBRARIES']['ember'] | ||||||
| source += env['LIBRARIES']['node-entry'] | ||||||
| source += env['LIBRARIES']['selfdrive'] | ||||||
|
|
||||||
| batt = env.StaticLibrary(node.lower(), source)[0] | ||||||
| firmware, flash = env.EspIdf(batt, 'esp32s3') | ||||||
|
|
||||||
| Return('firmware', 'flash') | ||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,234 @@ | ||||||
| #include "batt.h" | ||||||
|
|
||||||
| #include <ember_taskglue.h> | ||||||
| #include <esp_adc/adc_cali.h> | ||||||
| #include <esp_adc/adc_cali_scheme.h> | ||||||
| #include <esp_adc/adc_continuous.h> | ||||||
| #include <freertos/FreeRTOS.h> | ||||||
| #include <freertos/semphr.h> | ||||||
| #include <freertos/task.h> | ||||||
| #include <hal/adc_types.h> | ||||||
| #include <opencan_rx.h> | ||||||
| #include <opencan_tx.h> | ||||||
|
|
||||||
| #include <math.h> | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Include only the headers you need. |
||||||
|
|
||||||
| #define ADC_TASK_STACK_SIZE 2048 | ||||||
| #define SAMPLING_RATE_HZ 256 | ||||||
| #define SAMPLES 256 | ||||||
| #define FRAME_SAMPLES 128 | ||||||
| #define FRAME_SIZE \ | ||||||
| (sizeof(adc_digi_output_data_t) * FRAME_SAMPLES * ADC_CHANNELS) | ||||||
|
|
||||||
| #define SAMPLES_OUT_SIZE 1024 // how to decide the size of the output | ||||||
| #define PREV_SAMPLES_DELAY 0.010 // why is this the delay time | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To answer the comment, this was arbitrarily chosen :) |
||||||
| #define PREV_SAMPLES_SIZE ((size_t) (PREV_SAMPLES_DELAY * SAMPLING_RATE_HZ)) | ||||||
|
|
||||||
| #define POOL_SIZE (FRAME_SIZE * 2) | ||||||
|
|
||||||
| #define ADC_CONV ADC_CONV_SINGLE_UNIT_1 | ||||||
| #define ADC_DIGI_OUTPUT_FORMAT ADC_DIGI_OUTPUT_FORMAT_TYPE2 | ||||||
|
|
||||||
| #define BV_ADC_ATTEN ADC_ATTEN_DB_12 | ||||||
| #define BV_ADC_UNIT ADC_UNIT_2 | ||||||
| #define BV_ADC_CHANNEL ADC_CHANNEL_7 // GPIO_8 | ||||||
| #define BV_ADC_BITWIDTH SOC_ADC_DIGI_MAX_BITWIDTH | ||||||
|
Comment on lines
+29
to
+35
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||||||
|
|
||||||
| // #define PWM_PIN GPIO_NUM_37 | ||||||
| // #define PWM_FREQUENCY 1000 | ||||||
| // #define PWM_INIT_DUTY_CYCLE 0 | ||||||
| // #define PWM_RESOLUTION 10 | ||||||
|
|
||||||
| adc_continuous_handle_t handle = NULL; | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This will automatically be set to |
||||||
| static uint16_t out_buf[SAMPLES_OUT_SIZE]; | ||||||
| static size_t readIndex; | ||||||
| static size_t writeIndex; | ||||||
|
|
||||||
| // static ledc_timer_config_t pwm_timer = { | ||||||
| // .speed_mode = LEDC_LOW_SPEED_MODE, | ||||||
| // .duty_resolution = PWM_RESOLUTION, | ||||||
| // .timer_num = LEDC_TIMER_0, | ||||||
| // .freq_hz = PWM_FREQUENCY, | ||||||
| // }; | ||||||
| // | ||||||
| // static ledc_channel_config_t pwm_channel = { | ||||||
| // .gpio_num = PWM_PIN, | ||||||
| // .speed_mode = LEDC_LOW_SPEED_MODE, | ||||||
| // .channel = LEDC_CHANNEL_0, | ||||||
| // .intr_type = LEDC_INTR_DISABLE, | ||||||
| // .timer_sel = LEDC_TIMER_0, | ||||||
| // .duty = PWM_INIT_DUTY_CYCLE, | ||||||
| // }; | ||||||
|
|
||||||
| enum adc_channel_index { | ||||||
| BV_ADC_CHANNEL_INDEX, | ||||||
| ADC_CHANNELS, | ||||||
| }; | ||||||
|
|
||||||
| static uint8_t batteryPercent; | ||||||
| static void batt_init(); | ||||||
| static void batt_1Hz(); | ||||||
| static void adc_init(); | ||||||
|
|
||||||
| static void adc_task(); | ||||||
| static bool adc_callback(adc_continuous_handle_t handle, | ||||||
| const adc_continuous_evt_data_t *cbs, | ||||||
| void *user_data); | ||||||
|
|
||||||
| static void samples_out(); | ||||||
|
|
||||||
| struct samples { | ||||||
| uint16_t raw[SAMPLES]; | ||||||
| // float filtered[SAMPLES]; | ||||||
| size_t index; | ||||||
| }; | ||||||
|
|
||||||
| static struct { | ||||||
| SemaphoreHandle_t sem; | ||||||
| adc_continuous_handle_t handle; | ||||||
| TaskHandle_t task_handle; | ||||||
| struct samples samples[ADC_CHANNELS]; | ||||||
| } adc; | ||||||
|
|
||||||
| ember_rate_funcs_S module_rf = { | ||||||
| .call_init = batt_init, | ||||||
| .call_1Hz = batt_1Hz, | ||||||
| }; | ||||||
|
|
||||||
| static void batt_init() | ||||||
| { | ||||||
| // ledc_timer_config(&pwm_timer); | ||||||
| // ledc_channel_config(&pwm_channel); | ||||||
| adc_init(); | ||||||
| } | ||||||
|
|
||||||
| static void batt_1Hz() | ||||||
| { | ||||||
| printf("hi\n"); | ||||||
| batteryPercent = 0; | ||||||
| } | ||||||
|
|
||||||
| void CANTX_populate_BATT_BatteryStatus( | ||||||
| struct CAN_Message_BATT_BatteryStatus * const m) | ||||||
| { | ||||||
| m->BATT_batteryPercent = batteryPercent; | ||||||
| } | ||||||
|
|
||||||
| // initialization | ||||||
| static void adc_init() | ||||||
| { | ||||||
| // overflow = false; | ||||||
| adc.sem = xSemaphoreCreateBinary(); | ||||||
|
|
||||||
| // create task | ||||||
| xTaskCreatePinnedToCore(adc_task, | ||||||
| "adc_task", | ||||||
| ADC_TASK_STACK_SIZE, | ||||||
| NULL, | ||||||
| 1, | ||||||
| &adc.task_handle, | ||||||
| 1); | ||||||
|
|
||||||
|
|
||||||
| // adc resource allocation | ||||||
| adc_continuous_handle_cfg_t handle_config = { | ||||||
| .max_store_buf_size = POOL_SIZE, | ||||||
| .conv_frame_size = FRAME_SIZE, | ||||||
| }; | ||||||
|
|
||||||
| ESP_ERROR_CHECK( | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's get rid of the |
||||||
| adc_continuous_new_handle(&handle_config, &adc.handle)); | ||||||
|
|
||||||
| // configs for each ADC channel | ||||||
| adc_digi_pattern_config_t adc_pattern = { | ||||||
| .atten = BV_ADC_ATTEN, | ||||||
| .bit_width = BV_ADC_BITWIDTH, | ||||||
| .channel = BV_ADC_CHANNEL, | ||||||
| .unit = BV_ADC_UNIT, | ||||||
| }; | ||||||
|
|
||||||
| // adc IO configs to measure analog signal | ||||||
| adc_continuous_config_t io_config = { | ||||||
| .pattern_num = ADC_CHANNELS, | ||||||
| .adc_pattern = &adc_pattern, | ||||||
| .sample_freq_hz = SAMPLING_RATE_HZ, | ||||||
| .conv_mode = ADC_CONV, | ||||||
| .format = ADC_DIGI_OUTPUT_FORMAT, | ||||||
| }; | ||||||
|
|
||||||
| // invoking callbacks | ||||||
| adc_continuous_evt_cbs_t evt_cbs = { | ||||||
| .on_conv_done = adc_callback, | ||||||
| //.on_pool_ovf = overflow_callback, | ||||||
| }; | ||||||
|
|
||||||
| adc_continuous_register_event_callbacks(handle, &evt_cbs, NULL); | ||||||
| esp_err_t adc_continuous_start(handle); | ||||||
| } | ||||||
|
|
||||||
| static void adc_task() | ||||||
| { | ||||||
| loop: | ||||||
| if (xSemaphoreTake(adc.sem, portMAX_DELAY) != pdTRUE) { | ||||||
| goto loop; | ||||||
| } | ||||||
|
|
||||||
| static uint8_t frame_buf[FRAME_SIZE]; | ||||||
|
|
||||||
|
|
||||||
| static uint32_t out_length; | ||||||
|
|
||||||
| if (adc_continuous_read(handle, frame_buf, FRAME_SIZE, &out_length, 0) | ||||||
| != ESP_OK) { | ||||||
| goto loop; | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| adc_digi_output_data_t *samples; | ||||||
| size_t samples_count = out_length / sizeof(adc_digi_output_data_t); | ||||||
|
|
||||||
| readIndex = (writeIndex - PREV_SAMPLES_SIZE) % SAMPLES_OUT_SIZE; | ||||||
|
|
||||||
| for (size_t i = 0; i < samples_count; i++) { | ||||||
| // write catches up with read index | ||||||
| if (writeIndex == readIndex) { | ||||||
| samples_out(); | ||||||
| } | ||||||
| // uint16_t data = samples[i].type2.data; | ||||||
| // out_buf[writeIndex] = data; | ||||||
| writeIndex = (writeIndex + 1) % SAMPLES_OUT_SIZE; | ||||||
| } | ||||||
|
|
||||||
| goto loop; | ||||||
| } | ||||||
|
|
||||||
| static void samples_out() | ||||||
| { | ||||||
| printf("DIGI SAMPLES\n"); | ||||||
|
|
||||||
|
|
||||||
| static size_t total_samples = 0; | ||||||
| for (size_t i = 0; i < SAMPLES_OUT_SIZE; i++) { | ||||||
| printf("%u,%d\n", total_samples + i, out_buf[readIndex]); | ||||||
| readIndex = (readIndex + 1) % SAMPLES_OUT_SIZE; | ||||||
| } | ||||||
| readIndex = SIZE_MAX; | ||||||
| } | ||||||
|
|
||||||
| // ADC callback | ||||||
| static bool IRAM_ATTR adc_callback(adc_continuous_handle_t handle, | ||||||
| const adc_continuous_evt_data_t *cbs, | ||||||
| void *user_data) | ||||||
| { | ||||||
| // give back semaphore | ||||||
| xSemaphoreGiveFromISR(adc.sem, NULL); | ||||||
|
|
||||||
| return true; | ||||||
| } | ||||||
|
|
||||||
| // overflow callback | ||||||
| // static bool ram_attr overflow_callback(adc_continuous_handle_t handle, const | ||||||
| // adc_continuous_evt_data_t *cbs, void *user_data) { | ||||||
| // overflow = true; | ||||||
| // return true; | ||||||
| // } | ||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,4 @@ | ||||||
| #ifndef BATT_H | ||||||
| #define BATT_H | ||||||
|
|
||||||
| #endif // BATT.H | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../../../lib/firmware-base/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's try to keep this list in alphabetical order.