Skip to content

Commit 47fd048

Browse files
author
Richard Unger
committed
added a STM32MagneticSensorPWM
1 parent 11489d8 commit 47fd048

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

src/encoders/stm32pwmsensor/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
# STM32MagneticSensorPWM
3+
4+
STM32 MCU specific PWM sensor class. This class uses the STM32's hardware timers to precisely capture the PWM input signal, and doesn't use interrupts or have MCU overhead.
5+
6+
:warning: this code is not yet well tested.
7+
8+
## Setup
9+
10+
Please use an advanced control or general purpose timer pin on your STM32. Connect the sensor's PWM output to the MCU's input pin. Usually, pull-ups or pull-downs are not needed, but check your sensor's datasheet.
11+
12+
:warning: only tested on 16 bit timers. Code changes may be needed to make it work on 32 but timers. Avoid using TIM2 and TIM5 unless you want to test it.
13+
14+
The sensor needs the values `min_ticks` and `max-ticks` to be configured correctly to convert the PWM input into an angle. These values will depend on the sensor, but also on the MCU's timer clock speed.
15+
16+
To print the current tick value, use:
17+
18+
```
19+
sensor.getDutyCycleTicks();
20+
```
21+
22+
By rotating the motor through several full turns while printing the ticks to the screen you will be able to determine the correct values empirically.
23+
24+
## Usage
25+
26+
```
27+
STM32MagneticSensorPWM sensor = STM32MagneticSensorPWM(PB7, 412, 6917); // sample values, yours will be different
28+
29+
void setup() {
30+
...
31+
sensor.init();
32+
...
33+
}
34+
```
35+
36+
To use alternate function timers, set the PinName directly:
37+
38+
```
39+
sensor._pin = PB_7_ALT1; // manually set a PinName to use alternate timer function
40+
```
41+
42+
PWM sensor may have a slow update time (not more often than once per PWM-period, e.g. at the PWM frequency):
43+
44+
```
45+
sensor.min_elapsed_time = 0.001; // 1ms minimum sample time for velocity
46+
```
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
#include "./STM32MagneticSensorPWM.h"
3+
4+
#if defined(_STM32_DEF_)
5+
6+
#include "common/foc_utils.h"
7+
8+
9+
STM32MagneticSensorPWM::STM32MagneticSensorPWM(int pin, uint32_t _min_ticks, uint32_t _max_ticks) : STM32PWMInput(pin), max_ticks(_max_ticks), min_ticks(_min_ticks) {
10+
11+
};
12+
13+
14+
15+
STM32MagneticSensorPWM::~STM32MagneticSensorPWM(){};
16+
17+
18+
19+
void STM32MagneticSensorPWM::init(){
20+
initialized = (STM32PWMInput::initialize()==0);
21+
if(initialized)
22+
Sensor::init();
23+
};
24+
25+
26+
27+
float STM32MagneticSensorPWM::getSensorAngle(){
28+
uint32_t ticks = getDutyCycleTicks();
29+
return (ticks - min_ticks) * _2PI / (max_ticks - min_ticks);
30+
};
31+
32+
33+
34+
35+
#endif
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
#pragma once
3+
4+
5+
#include <Arduino.h>
6+
7+
8+
#if defined(_STM32_DEF_)
9+
10+
#include "common/base_classes/Sensor.h"
11+
#include "utilities/stm32pwm/STM32PWMInput.h"
12+
13+
14+
class STM32MagneticSensorPWM : public Sensor, public STM32PWMInput {
15+
public:
16+
STM32MagneticSensorPWM(int pin, uint32_t _min_ticks = 0, uint32_t _max_ticks = 0x0FFF);
17+
~STM32MagneticSensorPWM();
18+
19+
void init() override;
20+
21+
uint32_t max_ticks = 0x0FFF;
22+
uint32_t min_ticks = 0;
23+
bool initialized = false;
24+
protected:
25+
float getSensorAngle() override;
26+
};
27+
28+
#endif

0 commit comments

Comments
 (0)