Skip to content

Commit

Permalink
add atmega example for simplefocnano shield
Browse files Browse the repository at this point in the history
  • Loading branch information
runger1101001 committed Feb 14, 2024
1 parent 6468e52 commit 9cc5ab8
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 8 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/ccpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,41 +26,41 @@ jobs:
- arduino:mbed_rp2040:pico # rpi pico
include:
- arduino-boards-fqbn: arduino:avr:nano
sketches-exclude: calibrated mt6816_spi smoothing
sketches-exclude: calibrated mt6816_spi smoothing simplefocnano_torque_voltage
required-libraries: Simple FOC
- arduino-boards-fqbn: arduino:sam:arduino_due_x
required-libraries: Simple FOC
sketches-exclude: calibrated smoothing simplefocnano_torque_voltage
sketches-exclude: calibrated smoothing simplefocnano_torque_voltage simplefocnano_atmega
- arduino-boards-fqbn: arduino:samd:nano_33_iot
required-libraries: Simple FOC
sketches-exclude: calibrated smoothing
- arduino-boards-fqbn: arduino:mbed_rp2040:pico
required-libraries: Simple FOC
sketches-exclude: calibrated smoothing simplefocnano_torque_voltage
sketches-exclude: calibrated smoothing simplefocnano_torque_voltage simplefocnano_atmega
- arduino-boards-fqbn: adafruit:samd:adafruit_metro_m4
platform-url: https://adafruit.github.io/arduino-board-index/package_adafruit_index.json
required-libraries: Simple FOC
sketches-exclude: calibrated smoothing simplefocnano_torque_voltage
sketches-exclude: calibrated smoothing simplefocnano_torque_voltage simplefocnano_atmega
# - arduino-boards-fqbn: esp32:esp32:esp32doit-devkit-v1
# platform-url: https://dl.espressif.com/dl/package_esp32_index.json
# required-libraries: Simple FOC
# sketch-names: '**.ino'
- arduino-boards-fqbn: esp32:esp32:esp32 # esp32
platform-url: https://espressif.github.io/arduino-esp32/package_esp32_index.json
required-libraries: Simple FOC
sketches-exclude: calibrated smoothing simplefocnano_torque_voltage
sketches-exclude: calibrated smoothing simplefocnano_torque_voltage simplefocnano_atmega
- arduino-boards-fqbn: esp32:esp32:esp32s2 # esp32s2
platform-url: https://espressif.github.io/arduino-esp32/package_esp32_index.json
required-libraries: Simple FOC
sketches-exclude: calibrated smoothing simplefocnano_torque_voltage
sketches-exclude: calibrated smoothing simplefocnano_torque_voltage simplefocnano_atmega
- arduino-boards-fqbn: STMicroelectronics:stm32:GenF1:pnum=BLUEPILL_F103C8
platform-url: https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json
required-libraries: Simple FOC
sketches-exclude: calibrated mt6816_spi smoothing simplefocnano_torque_voltage
sketches-exclude: calibrated mt6816_spi smoothing simplefocnano_torque_voltage simplefocnano_atmega
- arduino-boards-fqbn: STMicroelectronics:stm32:Nucleo_64:pnum=NUCLEO_F411RE
platform-url: https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json
required-libraries: Simple FOC
sketches-exclude: smoothing simplefocnano_torque_voltage
sketches-exclude: smoothing simplefocnano_torque_voltage simplefocnano_atmega
# Do not cancel all jobs / architectures if one job fails
fail-fast: false
steps:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <Arduino.h>
#include <SPI.h>
#include "SimpleFOC.h"
#include "SimpleFOCDrivers.h"
#include "drivers/simplefocnano/SimpleFOCNanoDriver.h"
#include "encoders/as5048a/MagneticSensorAS5048A.h"


MagneticSensorAS5048A sensor = MagneticSensorAS5048A(PIN_nCS);
SimpleFOCNanoDriver driver = SimpleFOCNanoDriver();
BLDCMotor motor = BLDCMotor(11); // 11 pole pairs

long loopts = 0;
int iterations = 0;
float volts = 0.0f;

void setup() {
Serial.begin(115200); // enable serial port
delay(5000);
SimpleFOCDebug::enable(); // enable debug messages to Serial

sensor.init(); // init sensor on default SPI pins

// read voltage
SimpleFOCDebug::print("Bus voltage: ");
volts = driver.getBusVoltage(5.0f, 1024); // 5V reference, 10-bit ADC
SimpleFOCDebug::println(volts);
driver.voltage_power_supply = volts; // set driver voltage to measured value
driver.voltage_limit = 10.0f; // limit voltage to 10V
driver.pwm_frequency = 30000; // set pwm frequency to 30kHz
driver.init(); // init driver

motor.linkSensor(&sensor); // link the motor to the sensor
motor.linkDriver(&driver); // link the motor to the driver

motor.controller = MotionControlType::torque; // torque control
motor.torque_controller = TorqueControlType::voltage; // use voltage torque control
motor.voltage_limit = driver.voltage_limit / 2.0f; // limit voltage to 1/2 of driver limit
motor.voltage_sensor_align = 4.0f; // align voltage sensor to 4V

motor.init(); // init motor
delay(100); // wait for driver to power up
motor.initFOC(); // init FOC and calibrate motor

Serial.println("Motor ready.");
loopts = millis();

// Set the motor target to 2 volts
motor.target = 2.0f;
}


void loop() {
motor.move();
motor.loopFOC();
long now = millis();
iterations++;
if (now - loopts > 1000) {
Serial.print("Iterations/s: ");
Serial.println(iterations);
Serial.print("Angle: ");
Serial.println(sensor.getAngle());
loopts = now;
iterations = 0;
}
if (now - loopts < 0)
loopts = 0;
}

0 comments on commit 9cc5ab8

Please sign in to comment.