Skip to content

Commit 948a8f5

Browse files
author
Richard Unger
committed
add example and update test cases for SimpleFOCNano
1 parent 38413a5 commit 948a8f5

File tree

3 files changed

+81
-10
lines changed

3 files changed

+81
-10
lines changed

.github/workflows/ccpp.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
strategy:
1616
matrix:
1717
arduino-boards-fqbn:
18-
- arduino:avr:uno # arudino uno
18+
- arduino:avr:nano # arudino nano
1919
- arduino:sam:arduino_due_x # arduino due
2020
- arduino:samd:nano_33_iot # samd21
2121
- adafruit:samd:adafruit_metro_m4 # samd51
@@ -25,42 +25,42 @@ jobs:
2525
- STMicroelectronics:stm32:Nucleo_64:pnum=NUCLEO_F411RE # stm32 nucleo
2626
- arduino:mbed_rp2040:pico # rpi pico
2727
include:
28-
- arduino-boards-fqbn: arduino:avr:uno
28+
- arduino-boards-fqbn: arduino:avr:nano
2929
sketches-exclude: calibrated mt6816_spi smoothing
3030
required-libraries: Simple FOC
3131
- arduino-boards-fqbn: arduino:sam:arduino_due_x
3232
required-libraries: Simple FOC
33-
sketches-exclude: calibrated smoothing
33+
sketches-exclude: calibrated smoothing simplefocnano_torque_voltage
3434
- arduino-boards-fqbn: arduino:samd:nano_33_iot
3535
required-libraries: Simple FOC
3636
sketches-exclude: calibrated smoothing
3737
- arduino-boards-fqbn: arduino:mbed_rp2040:pico
3838
required-libraries: Simple FOC
39-
sketches-exclude: calibrated smoothing
39+
sketches-exclude: calibrated smoothing simplefocnano_torque_voltage
4040
- arduino-boards-fqbn: adafruit:samd:adafruit_metro_m4
4141
platform-url: https://adafruit.github.io/arduino-board-index/package_adafruit_index.json
4242
required-libraries: Simple FOC
43-
sketches-exclude: calibrated smoothing
43+
sketches-exclude: calibrated smoothing simplefocnano_torque_voltage
4444
# - arduino-boards-fqbn: esp32:esp32:esp32doit-devkit-v1
4545
# platform-url: https://dl.espressif.com/dl/package_esp32_index.json
4646
# required-libraries: Simple FOC
4747
# sketch-names: '**.ino'
4848
- arduino-boards-fqbn: esp32:esp32:esp32 # esp32
4949
platform-url: https://espressif.github.io/arduino-esp32/package_esp32_index.json
5050
required-libraries: Simple FOC
51-
sketches-exclude: calibrated smoothing
51+
sketches-exclude: calibrated smoothing simplefocnano_torque_voltage
5252
- arduino-boards-fqbn: esp32:esp32:esp32s2 # esp32s2
5353
platform-url: https://espressif.github.io/arduino-esp32/package_esp32_index.json
5454
required-libraries: Simple FOC
55-
sketches-exclude: calibrated smoothing
55+
sketches-exclude: calibrated smoothing simplefocnano_torque_voltage
5656
- arduino-boards-fqbn: STMicroelectronics:stm32:GenF1:pnum=BLUEPILL_F103C8
5757
platform-url: https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json
5858
required-libraries: Simple FOC
59-
sketches-exclude: calibrated mt6816_spi smoothing
59+
sketches-exclude: calibrated mt6816_spi smoothing simplefocnano_torque_voltage
6060
- arduino-boards-fqbn: STMicroelectronics:stm32:Nucleo_64:pnum=NUCLEO_F411RE
6161
platform-url: https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json
6262
required-libraries: Simple FOC
63-
sketches-exclude: smoothing
63+
sketches-exclude: smoothing simplefocnano_torque_voltage
6464
# Do not cancel all jobs / architectures if one job fails
6565
fail-fast: false
6666
steps:
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <Arduino.h>
2+
#include <SPI.h>
3+
#include "SimpleFOC.h"
4+
#include "SimpleFOCDrivers.h"
5+
#include "drivers/simplefocnano/SimpleFOCNanoDriver.h"
6+
#include "encoders/as5048a/MagneticSensorAS5048A.h"
7+
8+
9+
MagneticSensorAS5048A sensor = MagneticSensorAS5048A(PIN_nCS);
10+
SimpleFOCNanoDriver driver = SimpleFOCNanoDriver();
11+
BLDCMotor motor = BLDCMotor(11); // 11 pole pairs
12+
13+
Commander commander = Commander(Serial);
14+
void doMotor(char* cmd) { commander.motor(&motor, cmd); }
15+
16+
long loopts = 0;
17+
int iterations = 0;
18+
float volts = 0.0f;
19+
20+
void setup() {
21+
Serial.begin(115200); // enable serial port
22+
delay(5000);
23+
SimpleFOCDebug::enable(); // enable debug messages to Serial
24+
25+
sensor.init(); // init sensor on default SPI pins
26+
27+
// read voltage
28+
SimpleFOCDebug::print("Bus voltage: ");
29+
volts = driver.getBusVoltage(3.3, 4096);
30+
SimpleFOCDebug::println(volts);
31+
driver.voltage_power_supply = volts; // set driver voltage to measured value
32+
driver.voltage_limit = 10.0f; // limit voltage to 10V
33+
driver.pwm_frequency = 30000; // set pwm frequency to 30kHz
34+
driver.init(); // init driver
35+
36+
motor.linkSensor(&sensor); // link the motor to the sensor
37+
motor.linkDriver(&driver); // link the motor to the driver
38+
39+
motor.controller = MotionControlType::torque; // torque control
40+
motor.torque_controller = TorqueControlType::voltage; // use voltage torque control
41+
motor.voltage_limit = driver.voltage_limit / 2.0f; // limit voltage to 1/2 of driver limit
42+
motor.voltage_sensor_align = 4.0f; // align voltage sensor to 4V
43+
44+
motor.init(); // init motor
45+
delay(100); // wait for driver to power up
46+
motor.initFOC(); // init FOC and calibrate motor
47+
48+
commander.add('M', doMotor); // add motor command
49+
50+
Serial.println("Motor ready.");
51+
loopts = millis();
52+
}
53+
54+
55+
void loop() {
56+
motor.move();
57+
motor.loopFOC();
58+
commander.run();
59+
long now = millis();
60+
iterations++;
61+
if (now - loopts > 1000) {
62+
Serial.print("Iterations/s: ");
63+
Serial.println(iterations);
64+
Serial.print("Angle: ");
65+
Serial.println(sensor.getAngle());
66+
loopts = now;
67+
iterations = 0;
68+
}
69+
if (now - loopts < 0)
70+
loopts = 0;
71+
}

src/drivers/simplefocnano/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
# SimpleFOCNano Driver Class
33

4-
The `SimpleFOCNanoDriver` is a wrapper class around BLDCDriver3PWM to make using the [SimpleFOCNano shield](link) super-simple.
4+
The `SimpleFOCNanoDriver` is a wrapper class around BLDCDriver3PWM to make using the [SimpleFOCNano shield](https://github.com/simplefoc/SimpleFOCNano) super-simple.
55

66
If you use this driver you don't need to bother with any pin-numbers, they are all set correctly for you based on the SimpleFOCNano's pinout. Of course, this only works if you actually plug the shield into the Nano. If you use jumper wires, either make exactly the same connections as plugging in the shield would, or don't use this driver.
77

0 commit comments

Comments
 (0)