I made some tests on STM32, motor still shake alot after call motor.move(1) and readings seems fine from encoder 0 - 2PI using getSensorAngle, I kind of notice when debug that value from encoder sometimes jump a little and this probably cause it, can this be beacuse of SPI speed or not check CRC?
SimpleFoc Code:
#include <SimpleFOC.h>
#include "biss_c.h"
const uint16_t MOTOR_INH_A{PC6};
const uint16_t MOTOR_INH_B{PC7};
const uint16_t MOTOR_INH_C{PC8};
const uint16_t MOTOR_INL_A{PA7};
const uint16_t MOTOR_INL_B{PB0};
const uint16_t MOTOR_INL_C{PB1};
const uint16_t STATUS_LED{PA0};
const uint16_t OC_ADJ{PB5};
const uint16_t M_OC{PB6};
const uint16_t M_PWM{PB7};
const uint16_t nFAULT{PD2};
const uint16_t OC_GAIN{PA5};
const uint16_t EN_GATE{PB12};
BLDCMotor motor = BLDCMotor(6, 3.7);
BLDCDriver3PWM driver = BLDCDriver3PWM(MOTOR_INH_A, MOTOR_INH_B, MOTOR_INH_C, EN_GATE);
#define CLOCK_SPEED 1'000'000 // 1.0 MHz
encoder::biss_c encoder_bissc(SPI, 0x10000, CLOCK_SPEED, SPI_MODE1);
void setup()
{
encoder_bissc.init();
motor.linkSensor(&encoder_bissc);
pinMode(nFAULT, INPUT);
pinMode(M_OC,OUTPUT);
pinMode(M_PWM,OUTPUT);
pinMode(OC_ADJ,OUTPUT);
pinMode(OC_GAIN,OUTPUT);
pinMode(STATUS_LED, OUTPUT);
digitalWrite(STATUS_LED, LOW);
// DRV8302 specific code
digitalWrite(M_OC,LOW);
digitalWrite(M_PWM,HIGH);
digitalWrite(OC_ADJ,HIGH);
_delay(1000);
// power supply voltage [V]
driver.voltage_power_supply = 24;
driver.init();
motor.linkDriver(&driver);
motor.phase_resistance = 3.7;
motor.torque_controller = TorqueControlType::voltage;
motor.controller = MotionControlType::torque;
motor.init();
motor.initFOC();
_delay(1000);
}
void loop()
{
motor.loopFOC();
motor.move(1);
}
simple test driver
#include "biss_c.h"
namespace encoder
{
biss_c::biss_c(SPIClass& handler, uint32_t bitcount, uint32_t clock, uint8_t mode) :
spi_handler_(handler),
bitcount_(bitcount),
clock_speed_(clock),
mode_(mode)
{
}
void biss_c::init()
{
settings_ = SPISettings(clock_speed_, MSBFIRST, mode_);
spi_handler_.setSCLK(PC10);
spi_handler_.setMISO(PC11);
spi_handler_.setMOSI(PC12);
spi_handler_.begin();
Sensor::init();
}
float biss_c::getSensorAngle()
{
spi_handler_.beginTransaction(settings_);
spi_handler_.transfer(buff, 3);
spi_handler_.endTransaction();
uint16_t rawval16 = ((buff[0]&0x07)<<13) | (buff[1]<<5) | ((buff[2]&0xF8)>>3);
float angle_ = (float)rawval16 / bitcount_ * _2PI; // angle in radians
return angle_;
}
void biss_c::close()
{
spi_handler_.end();
}
}
#pragma once
#include <SPI.h>
#include <SimpleFOC.h>
namespace encoder
{
class biss_c :
public Sensor
{
public:
explicit biss_c(SPIClass& handler, uint32_t bitcount, uint32_t clock, uint8_t mode);
void init();
float getSensorAngle() override;
private:
void close();
SPIClass& spi_handler_;
SPISettings settings_;
uint32_t clock_speed_{0};
uint32_t bitcount_{0};
uint8_t mode_{0};
uint8_t buff[3] = { 0x00, 0x00, 0x00 };
};
}