AS5600 + drv8302 + SunnySky V2806 Shake

First of all, I would like to express my gratitude to the individuals responsible for creating this fantastic library. I am a university student in Japan, so I apologize in advance for any shortcomings in my English.

The system I am using consists of an Arduino Uno, a DRV8302 motor driver, an AS5600 magnetic sensor, and a SunnySky V2806 motor. The code I am using is based on a sample code provided in the documentation. The code I have implemented is nearly identical to the sample code.

/* ----- Pin setting -----
   PC4 - Analog   4 - Encoder SDA
   PC5 - Analog   5 - Encoder SCL
*/

#include <SimpleFOC.h>

// DRV8302 pins connections
// don't forget to connect the common ground pin
#define INH_A 5
#define INH_B 9
#define INH_C 11
#define INL_A 6
#define INL_B 10
#define INL_C 3
#define EN_GATE 8
#define M_PWM 6
#define M_OC 5
#define OC_ADJ 7

// motor instance
BLDCMotor motor = BLDCMotor(11);

// driver instance
BLDCDriver6PWM driver = BLDCDriver6PWM(INH_A, INL_A, INH_B, INL_B, INH_C, INL_C, EN_GATE);

MagneticSensorI2C sensor = MagneticSensorI2C(AS5600_I2C);

// commander interface
Commander command = Commander(Serial);
void onMotor(char *cmd) { command.motor(&motor, cmd); }

void setup()
{

  // configure i2C
  Wire.setClock(400000);
  // initialise magnetic sensor hardware
  sensor.init();

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

  // DRV8302 specific code
  // M_OC  - enable over-current protection
  pinMode(M_OC, OUTPUT);
  digitalWrite(M_OC, LOW);
  // M_PWM  - enable 6pwm mode (can be left open)
  pinMode(M_PWM, OUTPUT);
  digitalWrite(M_PWM, LOW);
  // OD_ADJ - set the maximum over-current limit possible
  // Better option would be to use voltage divisor to set exact value
  pinMode(OC_ADJ, OUTPUT);
  digitalWrite(OC_ADJ, HIGH);

  // configure driver
  driver.voltage_power_supply = 12;
  driver.init();
  motor.linkDriver(&driver);

  // choose FOC modulation
  motor.foc_modulation = FOCModulationType::SpaceVectorPWM;

  // set control loop type to be used
  motor.controller = MotionControlType::torque;

  // controller configuration based on the control type
  motor.PID_velocity.P = 0.2;
  motor.PID_velocity.I = 20;

  // velocity low pass filtering time constant
  motor.LPF_velocity.Tf = 0.01;

  // angle loop controller
  motor.P_angle.P = 20;

  // angle loop velocity limit
  motor.velocity_limit = 50;
  // default voltage_power_supply
  motor.voltage_limit = 12;

  // use monitoring with serial for motor init
  // monitoring port
  Serial.begin(115200);
  // comment out if not needed
  motor.useMonitoring(Serial);

  // initialize motor
  motor.init();
  // align encoder and start FOC
  motor.initFOC();

  // set the initial target value
  motor.target = 2;

  // define the motor id
  command.add('M', onMotor, "motor");

  _delay(1000);
}

// velocity set point variable
float target_velocity = 5; // 2Rad/s ~ 20rpm

void loop(){
  // iterative setting FOC phase voltage
  motor.loopFOC();

  // iterative function setting the outer loop target
  // velocity, position or voltage
  // if target not set in parameter uses motor.target variable
  motor.move(target_velocity);

  // user communication
  command.run();
}

Based on the serial monitor output, it appears to be functioning correctly, as shown below. However, in reality, the FAULT LED lights up and the operation comes to a halt, as demonstrated in the video (https://youtu.be/jb0efc9v35U).

MOT: Monitor enabled!
MOT: Init
MOT: Enable driver.
MOT: Align sensor.
MOT: sensor_direction==CCW
MOT: PP check: fail - estimated pp: 78.77
MOT: Zero elec. angle: 0.67
MOT: No current sense.
MOT: Ready.

I also attempted to run the “magnetic_sensor_i2c_example” code, which was mentioned in a previous question, but it didn’t seem to have any noticeable issues. I am unsure why this behavior is occurring.
I would greatly appreciate your assistance in resolving this matter.

Akitoshi (Saeki San?),

Welcome to the SimpleFOC forum, we hope you find what you need.

Could you please let me know which driver board are you using? I’m not sure which fault LED is lighting up. From the youtube video it appears one of the Chinese aliexpress boards, but the video is unclear and I don’t see which LED is that.

Thank you for using the library. I’m sure we will figure out what the problem is. Your English is excellent.

I would like to suggest you first try running an open loop and lower the voltage limit to about 2V. Please also specify/tell us about your power supply, BLDC motor parameters, etc. Most probably you over-current the board.

Cheers,
Valentine

1 Like

This is definitely a problem. It means the sensor is not working correctly. Do you have I2C pull-ups on SDA and SCL lines?

Is the sensor powered and working correctly? You could try a sketch to just read the sensor first, to make sure it is working…

1 Like

Hello @Valentine !
My name is Aktioshi Saeki.
In Japan, we use “san” for honorific titles, but it is nice to meet someone I know.

I apologize for the lack of necessary information.
This is the board I am using.

I am also using a SunnySky V2806 KV400.
I will try open loop.
Thanks. :man_bowing:

Hi @runger !
Thanks for your advice.
I have tested the hall sensor.
The code is pasted at the bottom.

2.19	0.00
2.19	0.00
2.19	0.00
2.19	0.00
2.19	0.00
2.19	-1.11
2.19	1.04

When I let the motor go around it changes roughly 2.19-8.52.
This seemed right to me.

#include <SimpleFOC.h>

// MagneticSensorI2C(uint8_t _chip_address, float _cpr, uint8_t _angle_register_msb)
//  chip_address  I2C chip address
//  bit_resolution  resolution of the sensor
//  angle_register_msb  angle read register msb
//  bits_used_msb  number of used bits in msb register
// 
// make sure to read the chip address and the chip angle register msb value from the datasheet
// also in most cases you will need external pull-ups on SDA and SCL lines!!!!!
//
// For AS5058B
// MagneticSensorI2C sensor = MagneticSensorI2C(0x40, 14, 0xFE, 8);

// Example of AS5600 configuration 
// MagneticSensorI2C sensor = MagneticSensorI2C(AS5600_I2C);

MagneticSensorI2C sensor = MagneticSensorI2C(AS5600_I2C);


void setup() {
  // monitoring port
  Serial.begin(115200);

  // configure i2C
  Wire.setClock(400000);
  // initialise magnetic sensor hardware
  sensor.init();

  Serial.println("Sensor ready");
  _delay(1000);
}

void loop() {
  // iterative function updating the sensor internal variables
  // it is usually called in motor.loopFOC()
  // this function reads the sensor hardware and 
  // has to be called before getAngle nad getVelocity
  sensor.update();
  
  // display the angle and the angular velocity to the terminal
  Serial.print(sensor.getAngle());
  Serial.print("\t");
  Serial.println(sensor.getVelocity());
}

Saeki San,

Sure, please try open loop, then let us know. For the open loop, make sure you run your motor at about 10 to 20% of your power supply voltage, else you will over-current and trip the board. Also, I see you set the current trip protection to the max, so this probably also contributes to the board and LED tripping.

Your motor has very low resistance and requires high current (20A) so make sure you don’t push too much current else you will burn the board, your board can supply only 15A. This is probably why the fault LED was lighting up. You either need a smaller motor, or a motor with higher resistance, or a more powerful board, or carefully adjust the current for SimpleFOC to stay under the board trip level.

Once you get the open loop running, we will investigate the sensor as @runger pointed out, once you are sure the sensor works, then you can combine the sensor and close the loop.

You need to take small steps and make sure at each step your code and components are working, else debugging a closed loop from scratch may be too difficult for you.

Please let me know if you need open loop sample code. I’m attaching it below. You need to modify the power supply voltage if it is different from 12V. Please edit and put your own. Let us know if any problems.

Cheers,
Valentine

#include <SimpleFOC.h>

#define INH_A 5
#define INH_B 9
#define INH_C 11
#define INL_A 6
#define INL_B 10
#define INL_C 3
#define EN_GATE 8
#define M_PWM 6
#define M_OC 5
#define OC_ADJ 7

BLDCMotor motor = BLDCMotor(1);

BLDCDriver6PWM driver = BLDCDriver6PWM(INH_A, INL_A, INH_B, INL_B, INH_C, INL_C, EN_GATE);

float power_supply_voltage = 12;
float target_velocity = 2.0;
float target_voltage = 2.0;

void setup() {

  // DRV8302 specific code
  // M_OC  - enable over-current protection
  pinMode(M_OC, OUTPUT);
  digitalWrite(M_OC, LOW);
  // M_PWM  - enable 6pwm mode (can be left open)
  pinMode(M_PWM, OUTPUT);
  digitalWrite(M_PWM, LOW);
  // OD_ADJ - set the maximum over-current limit possible
  // Better option would be to use voltage divisor to set exact value
  pinMode(OC_ADJ, OUTPUT);
  digitalWrite(OC_ADJ, HIGH);

  driver.voltage_power_supply = power_supply_voltage ;
  driver.pwm_frequency = 15000;
  driver.init();
  motor.linkDriver(&driver);
  motor.voltage_limit = target_voltage;   // [V]
  motor.velocity_limit = 100; // [rad/s]
  motor.controller = MotionControlType::velocity_openloop;
  motor.init();
  _delay(1000);

}

void loop() {
  motor.move(target_velocity); 
}
1 Like

@Valentine
Thank you for presenting the sample.
When I ran the sample, the motor seemed to shake.
I also changed the name of the motor to “BLDCMotor motor = BLDCMotor(14);”.
image

float target_velocity = 4.0;
float target_voltage = 4.0;

Here’s what it looks like after the change.
Why … :cry:

@konoe-akitoshi

You need to change the velocity and voltage targets until you get a smooth motion.

float target_velocity = 2.0;
float target_voltage = 2.0;

Try modifying the velocity and voltage to probably:

float target_velocity = 4.0;
float target_voltage = 4.0;

What is your power supply voltage?

Also, does the LED light up?

Please read this:

https://community.simplefoc.com/t/gimbal-motor-overheating/1034/6

Cheers,
Valentine

1 Like

Your motor is definitely spinning but the driver is tripping, so your code is working but your overcurrent protection is too sensitive.

Please increase or remove the overcurrent protection on the driver.

For open loop the number of pole pairs doesn’t matter. It will change the speed so please go back to

else you will get different speeds, your motor is trying to turn too fast because your pole pairs is incorrect. Also, your motor has 7 pole pairs, not 14.

For the close loop you must put

for the open loop you must up

The number of pole pairs = magnets / 2.

Cheers,
Valentine

1 Like

@Valentine Thank you for your advice.
I guess I did not understand about the open loop…
I tried it as you taught me.
I changed it like this.

float target_velocity = 8.0;
float target_voltage = 8.0;

Is this behavior correct?

@konoe-akitoshi

I cannot see if the FAULT light up from the video, can you make another video where the whole driver board is visible? I want to see the LED.

Also, lower the voltage and speed to 4.

float target_velocity = 4.0;
float target_voltage = 4.0;

It seems we are making good progress, yes.

Cheers,
Valentine

PS: Do you have an oscilloscope in your lab in Kyoto? It will help if you disconnect the motor, and only run the driver board without a motor and measure the output phases on the oscilloscope.

1 Like

Thank you so much.
I have modified and implemented the changes as you suggested.
As for the FAULT light, it is subtle.
It still seems to be there.
It’s sad… :cry:

I had forgotten about it.
I recently purchased a handheld oscilloscope so I can measure it. It’s late today so I’ll measure it tomorrow. Thanks.

@konoe-akitoshi

Saeki San,

Please don’t be sad.

No worry you are making really good progress, you are almost there!

Lower the voltage and speed to

float target_velocity = 1.0;
float target_voltage = 1.0;

And let’s try again.

We will make it work, no worry. It’s really late there, may be you are tired and need some sleep.

Do you have an oscilloscope in your lab in Kyoto? It will help if you disconnect the motor, and only run the driver board without a motor and measure the output phases on the oscilloscope.

Cheers,
Valentine

@Valentine Thanks for sticking with me for so long.
When changed to 1.0, it tries to rotate, but it moves back and forth, and does not seem to rotate.
https://community.simplefoc.com/t/as5600-drv8302-sunnysky-v2806-shake/3523/13?u=konoe-akitoshi
I will try to work on it tomorrow.
Thanks to you for making progress today.
Thank you very much.

Akitoshi

Saeki San,

Sure, let’s continue tomorrow.

We also have a discord, where you can real-time ask questions, we can try discord to avoid posting so many messages here, and I can see real-time what you do to help you.

おやすみなさい

Cheers,
Valentine

1 Like

Cheers,
Valentine