-
-
Notifications
You must be signed in to change notification settings - Fork 80
pbio/drv/battery/battery_ev3.c: Implement EV3 battery driver #375
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8bba198
pbio/drv/battery/battery_ev3.c: Implement EV3 battery driver
ArcaneNibble 01152af
pbio/drv/block_device/block_device_ev3.c: Read ADC once before booting
ArcaneNibble db23327
pbdrv: adc: ev3: Improve ADC settling time.
dlech b7f9b1e
pbio/sys/battery: add EV3 battery monitor
dlech File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
|
|
||
| #include <stdbool.h> | ||
|
|
||
| #include <pbdrv/adc.h> | ||
| #include <pbdrv/battery.h> | ||
| #include <pbio/error.h> | ||
| #include <pbdrv/gpio.h> | ||
|
|
@@ -15,22 +16,40 @@ | |
| #include <tiam1808/hw/hw_syscfg0_AM1808.h> | ||
|
|
||
| static const pbdrv_gpio_t battery_type_gpio = PBDRV_GPIO_EV3_PIN(19, 7, 4, 8, 8); | ||
| static const pbdrv_gpio_t battery_voltage_measure_en_gpio = PBDRV_GPIO_EV3_PIN(1, 7, 4, 0, 6); | ||
|
|
||
| #define PBDRV_EV3_BATTERY_VOLTAGE_ADC_CH 4 | ||
| #define PBDRV_EV3_BATTERY_CURRENT_ADC_CH 3 | ||
|
|
||
| void pbdrv_battery_init(void) { | ||
| pbdrv_gpio_alt(&battery_type_gpio, SYSCFG_PINMUX19_PINMUX19_7_4_GPIO8_8); | ||
| pbdrv_gpio_out_high(&battery_voltage_measure_en_gpio); | ||
| } | ||
|
|
||
| pbio_error_t pbdrv_battery_get_voltage_now(uint16_t *value) { | ||
| // TODO. Calculate battery voltage based on ADC channel 4. | ||
| // For now, return nominal value for now so we don't trigger low battery shutdown. | ||
| *value = 7200; | ||
| uint16_t value_raw; | ||
| pbio_error_t err = pbdrv_adc_get_ch(PBDRV_EV3_BATTERY_VOLTAGE_ADC_CH, &value_raw); | ||
| if (err != PBIO_SUCCESS) { | ||
| return err; | ||
| } | ||
| // Battery voltage is read through a voltage divider consisting of two | ||
| // 100 K resistors, which halves the voltage. The ADC returns 10 LSBs, | ||
| // where full scale is 5 V. | ||
| *value = ((uint32_t)(value_raw) * 2 * 5000) / 1023; | ||
| return PBIO_SUCCESS; | ||
| } | ||
|
|
||
| pbio_error_t pbdrv_battery_get_current_now(uint16_t *value) { | ||
| // TODO. Calculate battery current based on ADC channel 3. | ||
| *value = 0; | ||
| return PBIO_ERROR_NOT_IMPLEMENTED; | ||
| uint16_t value_raw; | ||
| pbio_error_t err = pbdrv_adc_get_ch(PBDRV_EV3_BATTERY_CURRENT_ADC_CH, &value_raw); | ||
| if (err != PBIO_SUCCESS) { | ||
| return err; | ||
| } | ||
| // Battery current is read across a 0.05 ohm equivalent shunt resistor | ||
| // which is then connected to an op-amp configured with a gain of 16 | ||
| // (non-inverting). This yields 1 A = 0.8 V. | ||
| *value = ((uint32_t)(value_raw) * 5000 * 10) / (1023 * 8); | ||
|
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. It would be nice to move scaling to mV to the ADC driver on all platforms. Can be a different PR though. I think David might be working on ADC at the moment. |
||
| return PBIO_SUCCESS; | ||
| } | ||
|
|
||
| pbio_error_t pbdrv_battery_get_type(pbdrv_battery_type_t *value) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
In the Linux kernel, for the battery voltage, I also took into account the VCE of Q19B that I somehow figured to be 50 mV (don't remember how I came up with that number). And I also added a bit more to account for the shunt resistor which depends on the current.
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.
After lowering the SCLK rate to 1MHz, this 50 mV offset seems to go away, so I might have figured that wrong in the Linux driver.
As far as the shunt resistor goes, it would probably be better to include that in the low battery shutdown battery level, but not in the battery voltage that we use for motor control. So for now, I guess we should just leave that out.