Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/encoders/calibrated_sensor/calibrated/calibrated.ino
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ BLDCDriver3PWM driver = BLDCDriver3PWM(PB4,PC7,PB10,PA9);
// instantiate the calibrated sensor object
// argument 1 - sensor object
// argument 2 - number of samples in the LUT (default 200)
// argument 3 - pointer to LUT array (if null, LUT will be filled only during calibration)
CalibratedSensor sensor_calibrated = CalibratedSensor(sensor);

// voltage set point variable
Expand Down Expand Up @@ -57,6 +58,8 @@ void setup() {
// Running calibration
// it will ouptut the LUT and the zero electrical angle to the serial monitor !!!!
sensor_calibrated.calibrate(motor);
// print the LUT to serial monitor
sensor_calibrated.printLUT(motor, Serial);

//Serial.println("Calibrating Sensor Done.");
// Linking sensor to motor object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@
*
* 1. Run this Sketch as is and wait for the calibration data to be generated and printed over Serial.
* 2. Then copy the output from Serial to calibrationLut, zero_electric_angle and sensor_direction
* 3. Change values_provided to true and run the Sketch again to see the motor skipping the calibration.
* 3. Run the same example again and the code will use the provided calibration data and skip the calibration procedure.
*/

#include <SimpleFOC.h>
#include <SimpleFOCDrivers.h>
#include "encoders/calibrated/CalibratedSensor.h"

// fill this array with the calibration values outputed by the calibration procedure
float calibrationLut[50] = {0};
// Replace these with the calibration data obtained from the first run
uint16_t calibrationLut[50] = {0};
float zero_electric_angle = 0;
Direction sensor_direction = Direction::UNKNOWN;

const bool values_provided = false;

// magnetic sensor instance - SPI
MagneticSensorSPI sensor = MagneticSensorSPI(AS5147_SPI, 14);
Expand All @@ -29,7 +28,10 @@ StepperDriver4PWM driver = StepperDriver4PWM(10, 9, 5, 6,8);
// argument 2 - number of samples in the LUT (default 200)
// argument 3 - pointer to the LUT array (defualt nullptr)
CalibratedSensor sensor_calibrated = CalibratedSensor(
sensor, sizeof(calibrationLut) / sizeof(calibrationLut[0]));
sensor,
sizeof(calibrationLut)/sizeof(calibrationLut[0]), // number of samples
sensor_direction==Direction::UNKNOWN ? nullptr : calibrationLut // pointer to LUT array
);

// voltage set point variable
float target_voltage = 2;
Expand Down Expand Up @@ -64,12 +66,17 @@ void setup() {
// initialize motor
motor.init();

if(values_provided) {
if(sensor_direction != Direction::UNKNOWN){
Serial.println(F("Using saved calibration data."));
// provide the saved zero angle and direction
motor.zero_electric_angle = zero_electric_angle;
motor.sensor_direction = sensor_direction;
} else {
// Running calibration
Serial.println(F("Running calibration..."));
sensor_calibrated.calibrate(motor);
// print the LUT to serial monitor
sensor_calibrated.printLUT(motor, Serial);
}

// Linking sensor to motor object
Expand Down
100 changes: 100 additions & 0 deletions src/comms/streams/BinaryIOUnbuffered.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// #include "./BinaryIOUnbuffered.h"

// BinaryIOUnbuffered::BinaryIOUnbuffered(Stream& io) : _io(io) {
// // nothing to do here
// };

// BinaryIOUnbuffered::~BinaryIOUnbuffered(){
// // nothing to do here
// };


// BinaryIOUnbuffered& BinaryIOUnbuffered::operator<<(float value) {
// _io.write((uint8_t*)&value, 4);
// return *this;
// };


// BinaryIOUnbuffered& BinaryIOUnbuffered::operator<<(uint32_t value) {
// _io.write((uint8_t*)&value, 4);
// return *this;
// };


// BinaryIOUnbuffered& BinaryIOUnbuffered::operator<<(uint8_t value) {
// _io.write(value);
// return *this;
// };


// BinaryIOUnbuffered& BinaryIOUnbuffered::operator<<(char value) {
// _io.write((uint8_t)value);
// return *this;
// };


// BinaryIOUnbuffered& BinaryIOUnbuffered::operator<<(Packet value) {
// if (value.type!=0x00) {
// _io.write(MARKER_BYTE);
// _io.write(value.payload_size+1);
// _io.write(value.type);
// }
// return *this;
// };


// BinaryIOUnbuffered& BinaryIOUnbuffered::operator<<(Separator value) {
// // separator is ignored in binary mode
// return *this;
// };


// // Immediate flush - no buffering, so nothing to do
// void BinaryIOUnbuffered::_flush() {
// // No internal buffer to flush
// };


// BinaryIOUnbuffered& BinaryIOUnbuffered::operator>>(float &value) {
// remaining -= _io.readBytes((uint8_t*)&value, 4);
// return *this;
// };


// BinaryIOUnbuffered& BinaryIOUnbuffered::operator>>(uint32_t &value) {
// remaining -= _io.readBytes((uint8_t*)&value, 4);
// return *this;
// };


// BinaryIOUnbuffered& BinaryIOUnbuffered::operator>>(uint8_t &value) {
// value = (uint8_t)_io.read();
// remaining--;
// return *this;
// };


// PacketIO& BinaryIOUnbuffered::operator>>(Packet& value) {
// while (!in_sync && _io.available() > 0) {
// if (_io.peek() == MARKER_BYTE)
// in_sync = true;
// else
// _io.read();
// }
// if (_io.peek() == MARKER_BYTE) {
// _io.read(); // discard the marker
// }
// if (!in_sync || _io.available() < 3) { // size, frame type, payload = 3 bytes minimum frame size
// value.type = 0x00;
// value.payload_size = 0;
// return *this;
// }
// value.payload_size = (uint8_t)_io.read() - 1;
// value.type = (uint8_t)_io.read();
// remaining = value.payload_size;
// return *this;
// };

// bool BinaryIOUnbuffered::is_complete() {
// return (remaining <= 0);
// };
34 changes: 34 additions & 0 deletions src/comms/streams/BinaryIOUnbuffered.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// #pragma once

// #include "../RegisterIO.h"
// #include <Stream.h>
// #include "Arduino.h"


// #define MARKER_BYTE 0xA5

// // Unbuffered Binary IO - writes data immediately without buffering
// // Useful for real-time applications where latency is critical
// // Trade-off: More serial writes (potentially slower overall) but lower latency

// class BinaryIOUnbuffered : public PacketIO {
// public:
// BinaryIOUnbuffered(Stream& io);
// virtual ~BinaryIOUnbuffered();
// BinaryIOUnbuffered& operator<<(float value) override;
// BinaryIOUnbuffered& operator<<(uint32_t value) override;
// BinaryIOUnbuffered& operator<<(uint8_t value) override;
// BinaryIOUnbuffered& operator<<(char value) override;
// BinaryIOUnbuffered& operator<<(Packet value) override;
// BinaryIOUnbuffered& operator<<(Separator value) override;
// BinaryIOUnbuffered& operator>>(float& value) override;
// BinaryIOUnbuffered& operator>>(uint32_t& value) override;
// BinaryIOUnbuffered& operator>>(uint8_t& value) override;
// PacketIO& operator>>(Packet& value) override;
// bool is_complete() override;
// virtual void _flush() override;

// protected:
// Stream& _io;
// uint8_t remaining = 0;
// };
Loading
Loading