Thanks for your reply, here is the code
#include <SimpleFOC.h>
#define PIN_IN_1 D5 //17//D5
#define PIN_IN_2 D6 //18//D6
#define PIN_IN_3 D7 //19//D7
#define PIN_EN D8 //20//D8
#define PIN_GND_1 D9 //21//D9
#define PIN_GND_2 D10 //5//D10
#define PIN_CS D2 //25//D2
#define PIN_POT_GND A0 //26//A0
#define PIN_POT_SGN A1 //27//A1
#define PIN_POT_5V A2 //28//A2
#define PIN_STATE_TOGGLE D4 //16//D4
#define PHASE_RESISTANCE 4.5 //Ohm
#define KV 70 //RPM/V
#define PHASE_INDUCTANCE 0.001800//[H]
// BLDC motor & driver instance
// BLDCMotor( pp number , phase resistance, KV rating)
BLDCMotor motor = BLDCMotor(14,PHASE_RESISTANCE , KV,PHASE_INDUCTANCE);
//BLDCMotor motor = BLDCMotor(14);
BLDCDriver3PWM driver = BLDCDriver3PWM(PIN_IN_1, PIN_IN_2, PIN_IN_3, PIN_EN);
// Magnetic sensor instance
MagneticSensorSPI AS5x4x = MagneticSensorSPI(AS5048_SPI, PIN_CS);
// instantiate the commander
//Commander command = Commander(Serial);
unsigned int potV = 0;
unsigned int POT_MIN = 0;
unsigned int POT_MAX = 1024;
float SPEED_MIN = 0; //Rad/s
float SPEED_MAX = 106; //rad/s
float POS_MIN = 0;
float POS_MAX = 6.28;
float mappedV = 0;
unsigned long currentTime = 0;
unsigned long previousMillis = 0; // will store last time LED was updated
unsigned long timeDifferenceMS = 0;
unsigned long DISPLAY_TIMER_MS = 2000;
int ledState = LOW; // ledState used to set the LED
bool stateToggle = LOW;
float mapFloat(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
void setup() {
pinMode(PIN_GND_1, OUTPUT);
pinMode(PIN_GND_2, OUTPUT);
pinMode(PIN_POT_GND,OUTPUT);
pinMode(PIN_POT_5V,OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(PIN_STATE_TOGGLE,OUTPUT);
digitalWrite(PIN_GND_1, LOW);
digitalWrite(PIN_GND_2, LOW);
digitalWrite(PIN_POT_GND, LOW);
digitalWrite(PIN_POT_5V, HIGH);
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(PIN_STATE_TOGGLE,stateToggle);
Serial.begin(9600);
while (!Serial);
Serial.println("Start of setup");
//Serial.println("Arduino MbedOS");
Serial.println("Arduino-pico");
digitalWrite(LED_BUILTIN, LOW);
// initialize magnetic sensor hardware
AS5x4x.init();
// link the motor to the sensor
motor.linkSensor(&AS5x4x);
Serial.println("sensor done");
// driver config
// power supply voltage [V]
driver.voltage_power_supply = 16;
driver.init();
// link the motor and the driver
motor.linkDriver(&driver);
Serial.println("driver done");
// set motion control loop to be used
// open loop control config
//motor.controller = MotionControlType::velocity_openloop;
motor.controller = MotionControlType::velocity;
//motor.controller = MotionControlType::angle;
// choose FOC modulation
motor.foc_modulation = FOCModulationType::SinePWM; //(default)
//motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
//motor.foc_modulation = FOCModulationType::Trapezoid_120;
//motor.foc_modulation = FOCModulationType::Trapezoid_150;
// velocity PID controller parameters
// default P=0.5 I = 10 D =0
motor.PID_velocity.P = 0.01;// 0.01//0.5;
motor.PID_velocity.I = 0.1;//1 //10;
motor.PID_velocity.D = 0;
// jerk control using voltage voltage ramp
// default value is 300 volts per sec ~ 0.3V per millisecond
motor.PID_velocity.output_ramp = 1000;
//In case of ANGLE controller
// angle P controller
motor.P_angle.P = 20;
// maximal velocity of the position control
motor.velocity_limit = 106;//6.28;
// velocity low pass filtering
// default 5ms - try different values to see what is the best.
// the lower the less filtered
motor.LPF_velocity.Tf = 0.01;
// downsampling value
motor.motion_downsample = 0; // - times (default 0 - disabled)
// init motor hardware
// limiting motor current (provided resistance)
motor.current_limit = 0.5; //2 // [Amps]
//motor.voltage_limit = motor.current_limit*PHASE_RESISTANCE;
//driver.enable();
motor.init();
Serial.println("motor init done");
motor.initFOC();
Serial.println("motorInitFOC done");
Serial.println("End of setup");
//digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
}
void loop() {
stateToggle = !stateToggle;
digitalWrite(PIN_STATE_TOGGLE,stateToggle);
motor.loopFOC();
potV = analogRead(PIN_POT_SGN);
potV = (potV < 10)?0:potV;
if(motor.controller == MotionControlType::angle){
mappedV = mapFloat(potV,POT_MIN,POT_MAX,POS_MIN,POS_MAX);
}
else{
mappedV = mapFloat(potV,POT_MIN,POT_MAX,SPEED_MIN,SPEED_MAX);
}
//Serial.println(mappedV);
motor.move(mappedV);
//driver.setPwm(8, 4, 2);
}
It is the same as the one I was running on the Arduino Uno, except for some pins.
As I said, I am not able to communicate with the encoder using the MagneticSensorSPI class, even though I am able to do this using code :
AS5048A Arduino library
I use Arduino IDE 2.3.3, earlephilhower arduino pico 3.1.1, simple FOC 2.3.4 .
Target is a Arduino nano rp2040 connect.
As you can see every loop function call toggles a pin, that I monitor from an oscilloscope.
I will try later on an raspberry pi pico.
I also ordered different mcu, will try on a teensy 4.0.