Need help figuring out DRV 8302 + as5600 + Arduino Uno

I am desperately trying to get the DRV 8302 Driver, an Arduino Uno, an as5600 magnetic encoder, and my Propdrive V2 28-36 1000Kv to function. I am trying to make a robot dog, but before I continue with the design I need to verify this motors capability before I spend more time and money on making the rest of the dog. I have looked at various examples and other peoples code, and here is what I have found to work (not give me errors and provide an output)

#include <SimpleFOC.h>

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

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

// driver instance
BLDCDriver3PWM driver = BLDCDriver3PWM(INH_A, INH_B, INH_C, EN_GATE);

// Changed encoder to magnetic encoder
MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0x0E, 4);

void setup() {

  
  // initialize magnetic sensor instead of regular encoder
  sensor.init();

  motor.linkSensor(&sensor);

  // DRV8302 specific code
  // M_OC  - enable over-current protection
  pinMode(M_OC,OUTPUT);
  digitalWrite(M_OC,LOW);
  // M_PWM  - enable 3pwm mode
  pinMode(M_PWM,OUTPUT);
  digitalWrite(M_PWM,HIGH);
  // 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 = 10; //Using exernal power supply set on 10 volt with current limit set to 1 amps.
  driver.init();
  motor.linkDriver(&driver);

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

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

motor.useMonitoring(Serial);

  // 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 = 1; //lowered the motor voltage due to the low internal resistance of the motor

  // 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();
  
  _delay(1000);
}


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(1);
   motor.monitor();

  // user communication
//  motor.command(serialReceiveUserCommand());
}

// utility function enabling serial communication the user
String serialReceiveUserCommand() {
  
  // a string to hold incoming data
  static String received_chars;
  
  String command = "";

  while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the string buffer:
received_chars += inChar;

// end of user input
if (inChar == '\n') {
  
  // execute the user command
  command = received_chars;

  // reset the command buffer 
  received_chars = "";
}}}

//  return command;

It provides me with this as the output in the serial monitor:

MOT: Monitor enabled!
MOT: Iniensor.

MOT: Monitor enabled!
MOT: Init
MOT: Enable driver.
MOT: Align sensor.
MOT: sensor_direction==CCW
MOT: PP check: fail - estimated pp: 15.46
MOT: Zero elec. angle: 2.73
MOT: No current sense.
MOT: Ready.
1.0000	1.0000	0.0000	-1.2410
1.0000	1.0000	0.0000	-1.2410
1.0000	1.0000	0.0000	-1.2410
1.0000	1.0000	0.0000	-1.2410
1.0000	1.0000	0.0000	-1.2410
1.0000	1.0000	0.0000	-1.2410
1.0000	1.0000	0.0000	-1.2410
1.0000	1.0000	0.0000	-1.2410
1.0000	1.0000	0.0000	-1.2410
1.0000	1.0000	0.0000	-1.2410
1.0000	1.0000	0.0000	-1.2410
1.0000	1.0000	0.0000	-1.2410
1.0000	1.0000	0.0000	-1.2410
1.0000	1.0000	0.0000	-1.2410
1.0000	1.0000	0.0000	-1.2410
1.0000	1.0000	0.0000	-1.2410
1.0000	1.0000	0.0000	-1.2410
1.0000	1.0000	0.0000	-1.2410
1.0000	1.0000	0.0000	-1.2410
1.0000	1.0000	0.0000	-1.2410
1.0000	1.0000	0.0000	-1.2410
1.0000	1.0000	0.0000	-1.2410
1.0000	1.0000	0.0000	-1.2410
1.0000	1.0000	0.0000	-1.2410
1.0000	1.0000	0.0000	-1.2410
1.0000	1.0000	0.0000	-1.2410
1.0000	1.0000	0.0000	-1.2410
1.0000	1.0000	0.0000	-1.2410

When I rotate my motor the rightmost column seems to be reading the position data while the second from the right seems to be rotational speed.

My apologies for how bad I am at this. I am adept at MATLAB, but never learned C and its really coming to bite me right now. Does anybody know what to type into the serial monitor to command the program to rotate a certain amount or anything? I am unsure if the code is not working, or I just simply don’t know the control commands to input. Most likely both.

hi @Daniel_Amuedo , welcome to SimpleFOC!

Looks like you’re off to a decent start!

A few comments: this is not the simplest motor to start with… it’s 1000KV, 30A, 400W stats indicate this is a fast-turning motor, if you run it at its rated 3S-4S voltage, lets call it 12V you’d get 12000RPM just going by the KV and 30A will probably require current sensing. I wonder if the Arduino UNO will have the bandwidth to keep it under control. I think you’ll want a faster MCU.

And I think you’ll definately want a faster sensor. Any I2C sensor won’t update much faster than 1kHz, and will therefore limit your main loop speed to this. But to have good control at 10000RPM you’ll want a faster control loop than that, and less latency on the sensor data.

All that said you can still see if you can get it working. You’ll have to keep limits low initially to keep things controllable, and you can push them outward as you get things working well. Starting with a motor.voltage_limit = 1; might be enough, but you might want to set it even lower. And also set the driver. voltage_limit to a low value, as a safety.

motor.PID_velocity.I = 20; seems very high, I’d start with a lower value. Generally speaking, to tune, start all the values for P, I, D at 0, and slowly raise P until you get oscillations, then take it back a bit. Then slowly raise I until the motor tracks the set point well. Generally, leave D at 0.

All this becomes relevant for the velocity and position modes.

The LPF Tf and the ramp value can also have a big effect on the tuning.

Which 8302 driver board do you have?

For the communications, please have a look at our “Commander” code. The serialRecieveUserCommand business is slightly older code, and you’ve commented it out at the moment anyway.
If memory allows, you can add the “commander” and then send commands via Arduino’s serial monitor.