Skip to content

Commit

Permalink
added the adc choosing guide
Browse files Browse the repository at this point in the history
  • Loading branch information
askuric committed Apr 26, 2024
1 parent ead1190 commit 1ebf74d
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 18 deletions.
18 changes: 8 additions & 10 deletions docs/simplefoc_library/code/current_sense/low_side.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Low side current sensing for all the architectures is on our road-map and we are

## Current sensing support per MCU architecture

Low side current sensing is currently supported for several MCU architectures supported by the <span class="simple">Simple<span class="foc">FOC</span>library</span>. ESP32 architecture has the most generic support, supporting multiple motors per chip. Stm32 families f1, f4 and g4 are initially supported and support low-side sensing for only one motor. A special case of the STM32 board is the B-G431-ESC1 development kit which has very specific low-side implementation for its hardware configuration, and it is fully supported by the library. Samd21 architecture is under development, it has an initial support for only one motor, but for now as it has not been extensively tested, we suggest not to rely on our implementation.
Low side current sensing is currently supported for several MCU architectures supported by the <span class="simple">Simple<span class="foc">FOC</span>library</span>. ESP32 architecture has the most generic support, supporting multiple motors per chip. Stm32 families f1, f4, l4, g4 and f7 are supported and support low-side sensing for only one motor. A special case of the STM32 board is the B-G431-ESC1 development kit which has very specific low-side implementation for its hardware configuration, and it is fully supported by the library. Samd21 architecture is under development, it has an initial support for only one motor, but for now as it has not been extensively tested, we suggest not to rely on our implementation. Teensy4 has an initial support for low-side sensing for one motor as well.

MCU | Low-side Current sensing
--- | ---
Expand Down Expand Up @@ -49,7 +49,7 @@ Renesas (UNO R4) | ❌ (TBD)

Low-side current sensing requires very high synchronisation of the PWM generated by the `driver` and the ADC triggering. There are two main considerations to have in mind when choosing your `driver` parameters:
1. PWM frequency considerations
2. PWM pins
2. Appropriate PWM and ADC pin considerations


See more info about driver paramers in the [driver docs](drivers_config)!
Expand All @@ -69,17 +69,15 @@ driver.pwm_frequency = 20000;
</code>
</blockquote>

####  2. PWM pin considerations

As ADC conversion has to be synchronised with the PWM generated on ALL the phases, it is important that all the PWM generated for all the phases have aligned PWM. Since the microcontrollers usually have more than one timer for PWM generation on its pins, different architectures of microcontrollers have different degrees of alignment in between the PWM generated from different timers.
####  2. Appropriate PWM and ADC pin considerations

As ADC conversion has to be synchronised with the PWM generated on ALL the phases, it is important that all the PWM generated for all the phases have aligned PWM and that it supports triggering of the ADC conversion. Since the microcontrollers usually have more than one timer for PWM generation and more than one ADC for reading the analog values it is important to choose the right pins for the right phase.

<blockquote class="info">
<p class="heading">RULE OF THUMB: PWM timer pins</p>
In order to maximise your chances for the low-side current sensing to work well we suggest to make sure that the PWM pins chosen for your driver all belong to the same Timer.

Finding out which pins belong to different timers might require some time to be spent in the MCU datasheet 😄
You can also always ask the community for help - <a href="https://community.simplefoc.com/">community link</a>!
<p class="heading">RULE OF THUMB: PWM timer and ADC pins</p>
In order to maximise your chances for the low-side current sensing to work well we suggest to make sure that the PWM pins chosen for your driver all belong to the same Timer, and the ADC pins chosen for your current sense all belong to the same ADC.<br>
📢 Here is a quick guide to choosing appropriate PWM pins for different MCU architectures <a href="choosing_pwm_pins">see in docs</a>.<br>
📢 Here is a quick guide to choosing appropriate ADC pins for different MCU architectures <a href="choosing_adc_pins">see in docs</a>.
</blockquote>


Expand Down
15 changes: 9 additions & 6 deletions docs/simplefoc_library/digging_deeper/practical/choosing_pins.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,10 @@ ESP32 | ✔️
ESP32-S2| ❌
ESP32-S3| ✔️
ESP32-C3 | ❌
ESP32-C6 | ✔️
ESP32-H2 | ✔️
ESP32-P4 | ✔️

<!-- ESP32-C6 | ?
ESP32-H2 | ?
ESP32-P4 | ? -->

All the esp32 architecture that have the `MCPWM` will by default use `MCPWM` for motor control.

Expand All @@ -139,9 +140,10 @@ ESP32 | 16
ESP32-S2|8
ESP32-S3|8
ESP32-C3|6
ESP32-C6|6
ESP32-H2|6
ESP32-P4|8

<!-- ESP32-C6|6 (no arduino support yet)
ESP32-H2|6 (no arduino support yet)
ESP32-P4|8 (no arduino support yet) -->

If the user wants to force using the `LEDC` driver for the boards that support `MCPRM` he has to set the `SIMPLEFOC_ESP32_USELEDC`.

Expand Down Expand Up @@ -359,3 +361,4 @@ FlexTimer | submodule | A | B | X
`FlexPWM4`|2| 2 | 3 | -

</div>

113 changes: 113 additions & 0 deletions docs/simplefoc_library/digging_deeper/practical/choosing_pins_adc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
---
layout: default
title: Choosing ADC pins
nav_order: 4
description: "Arduino Simple Field Oriented Control (FOC) library ."
permalink: /choosing_adc_pins
parent: Practical guides
grand_parent: Digging deeper
grand_grand_parent: Arduino <span class="simple">Simple<span class="foc">FOC</span>library</span>
---

# A short guide to choosing Analog pins for your application

When it comes to choosing pins for your application, there are a few things to consider. The most important thing is to make sure that the pins you choose are compatible with the hardware you are using.
Make sure to check that your microcontroller family and SimpleFOC supports the necessary PWM generation, position sensor and current sensing technique. You can find this info in our docs [here](microcontrollers).

Once you are sure that you microcontroller is apt to the application, it is time to consider which pins to use. There are three main questions:
1. Which pins to use for PWM generation?
2. Which pins to use for the position sensor?
3. Which pins to use for current sensing?

In this guide we will focus on the first question: **Which analog pins to use for current sensing?**

The answer to these questions depends on the microcontroller you are using and the hardware you have. Here is the list of microcontroller families supported by SimpleFOC and the PWM generation modes supported by each of them:

MCU | In-line | Low-side | High-side
--- | --- |--- |---
Arduino AVR (8-bit) | ✔️ | ❌ | ❌
Arduino DUE | ✔️ | ❌ | ❌
stm32 (in general) | ✔️ | ❌ | ❌
stm32f1 family | ✔️ | ✔️ (one motor) | ❌
stm32f4 family | ✔️ | ✔️ (one motor) | ❌
stm32g4 family | ✔️ | ✔️ (one motor) | ❌
stm32l4 family | ✔️ | ✔️ (one motor) | ❌
stm32f7 family | ✔️ | ✔️ (initial) | ❌
stm32 B_G431B_ESC1 | ❌ | ✔️ (one motor) | ❌
esp32/esp32s3 | ✔️ | ✔️ | ❌
esp32s2/esp32c3 | ✔️ | ❌ | ❌
esp8266 | ❌ | ❌ | ❌
samd21 | ✔️ | ✔️ (one motor) | ❌
samd51 | ✔️ | ❌ | ❌
teensy3 | ✔️ | ❌ | ❌
teensy4 | ✔️ | ✔️(one motor) | ❌
Raspberry Pi Pico | ✔️ | ❌ | ❌
Portenta H7 | ✔️ | ❌ | ❌
nRF52 | ✔️ | ❌ | ❌
Renesas (UNO R4 Minima) | ❌ | ❌ | ❌

### In-line current sensing
What is important to see is that most of the the microcontroller families, except esp8266 and Arduino UNO R4, can be used with in-line current sensing. For this current sensing technique, you can use any of the analog pins, as no synchronization between the PWM signals and the current sensing is necessary.

### Low-side current sensing
When it comes to using the low-side current sensing, a precise synchronisation between the PWM signals and the current sensing is necessary ([read more here](low_side_current_sense)).
SimpleFOC aims to support low-side current sensing for all the microcontroller families that have the capability to synchronise the ADC conversion with the PWM signals.
For the moment, the low-side current sensing is supported for the following microcontroller families: stm32, esp32, teensy4 and samd21.


<blockquote class="info" markdown="1"><p class="heading">What about high-side current sensing?</p>
High-side current sensing is not supported by SimpleFOC at the moment. This is mostly because the high-side sensing is very rare in practice. Most of the mcu families that can support low-side sensing could support the high side as well, but as of now there is no support for it in the library. If you are interested in this feature, please let us know on our community forum.
</blockquote>

When it comes to choosing the analog pins for the low-side current sensing, as there is a need for the synchronization between the PWM signals (belonging to one or more timers) and the ADC conversion, the choice of the pins is more limited. **As a rule of thumb, it is recommended to use the pins that belong to the same ADC.** This comes form the fact that in the PWM synchronization process, timers often can trigger only one ADC at a time.

The information about the ADC associated with the pins is sometimes hard to obtain, especially for less experienced users. So in the next few sections we will provide some information on how to find the ADC pins for some of the most popular microcontroller families supporting the low-side current sensing.

- [ESP32 boards](#esp32-boards)
- [STM32 boards](#stm32-boards)
- [Teensy4 boards](#teensy4-boards)

## ESP32 boards

Esp32 boards have two ADCs that can be used for the low-side current sensing. The ADCs are associated with the pins in the following way (info from the [espressif docs](https://docs.espressif.com/projects/esp-idf/en/v4.4/esp32/api-reference/peripherals/adc.html)):


ESP32 SoC |`ADC1` pins | `ADC2` pins
--- | --- |---
ESP32 | GPIO32 - GPIO39 | GPIO0, GPIO2, GPIO4, GPIO12 - GPIO15, GOIO25 - GPIO27
ESP32-S2| GPIO1 - GPIO10 | GPIO11 - GPIO20
ESP32-S3| GPIO1 - GPIO10 | GPIO11 - GPIO20
ESP32-C3 | GPIO0 - GPIO4 | GPIO5

<blockquote class="info" markdown="1"><p class="heading">Important</p>
Esp32 has a very flexible ADC configuration, so you can use any of the pins listed above for the low-side current sensing. However, it is recommended to use the pins that belong to the same `ADC`.
</blockquote>


## STM32 boards

Stm32 is the most powerful family of microcontrollers supported by SimpleFOC, at least in terms of motor control capabilities. SimpleFOC supports many of the STM32 families such as stm32f1, stm32f4, stm32g4, stm32l4, stm32f7, and all of them can be used in low-side current sensing mode.

<blockquote class="info" markdown="1"><p class="heading">Important</p>
For low-side current sensing with stm32 boards, it is required to use the analog pins that belong to the same `ADC`.
</blockquote>

Once you provide the SimpleFOC current sense object with a set of pins, the library will automatically associate the pins with the appropriate ADC and channels and will take care of the synchronization between the PWM signals and the ADC conversion. If the pins do not belong to the same ADC, the library will throw an error.

Finding the pins that belong to the same ADC is not an easy task, especially for less experienced users. Therefore, we've created a website that enables a relatively easy navigation of the pins and timers for the most popular stm32 boards.

<a href ="https://docs.simplefoc.com/stm32pinouts/" class="btn btn-primary"><i class="fa fa-github"></i> Open stm32 pinout helper</a>

## Teensy4 boards

Teensy4 boards are very powerful and have a lot of analog pins that can be used for with both of their ADCs. The pins that belong to the ADCs are listed in the table below. For more info see the awesome github repo [TeensyDocuments](https://github.com/KurtE/TeensyDocuments/blob/master/Teensy4%20Pins.pdf)

Board | `ADC1` pins only | `ADC2` pins only | Both ADCs
---| ---- | ---- | ----
Teensy 4 | 24, 25 | 26, 27 | 14, 15,..., 22, 23, 40, 41
Teensy 4.1 | 24, 25 | 26, 27, 38, 39 | 14, 15,..., 22,23


<blockquote class="info" markdown="1"><p class="heading">Important</p>
For low-side current sensing with Teensy4 boards, it is required to use the analog pins that belong to the `ADC1`.
</blockquote>
8 changes: 7 additions & 1 deletion docs/simplefoc_library/digging_deeper/practical/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ Additionally, there are many different useful practical information already avai
<a href="choosing_pwm_pins">
<h3 style="color:inherit"> <i class="fa fa-lg fa-wrench" style="padding:10px"></i> Choosing PWM pins for you application</h3>
</a>
<p> A short guide to choosing appropriate pins for your application </p>
<p> A short guide to choosing appropriate PWM pins for your application </p>
</div>
<div >
<a href="choosing_adc_pins">
<h3 style="color:inherit"> <i class="fa fa-lg fa-wrench" style="padding:10px"></i> Choosing Analog pins for you application</h3>
</a>
<p> A short guide to choosing appropriate analog pins for your application </p>
</div>
<div>
2 changes: 1 addition & 1 deletion docs/simplefoc_library/hardware/mcus/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ The current sensing support for all the architectures is shown in the table belo

MCU | In-line | Low-side | High-side
--- | --- |--- |---
Arduino (8-bit) | ✔️ | ❌ | ❌
Arduino AVR (8-bit) | ✔️ | ❌ | ❌
Arduino DUE | ✔️ | ❌ | ❌
stm32 (in general) | ✔️ | ❌ | ❌
stm32f1 family | ✔️ | ✔️ (one motor) | ❌
Expand Down

0 comments on commit 1ebf74d

Please sign in to comment.