SimpleFOCStudio Base Code

Is there base micro controller code or an a example that does closed loop position control while working with SimpleFOCStudio?

I have tried the “angle_control.ino”. It initializes correctly but then doesn’t not seem to allow communication/control and the graphs do not do anything when run is clicked. The motor also seems to sit there oscillating.

I’ve had varying amounts of failure and cant find any documentation on how to use SimpleFOCStudio effectively.

Using a FOC shield plugged into an Uno with an 8 pole Nema17 BLDC and A5048 SPI encoder.

Hi @MichaelC

Before trying to use the SimpleFOCStudio I would recommend that you get it to work in open loop velocity mode to be sure that the your setup works correctly. This mode does not need any command if you set an initial target.

This code is taken from documentation pageremember to add M to the GUI otherwise it will not work

image

#include <SimpleFOC.h>

....

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

void setup(){
  ....
  // add the motor to the commander interface
  // The letter (here 'M') you will provide to the SimpleFOCStudio
  command.add('M',doMotor,'motor');
  // tell the motor to use the monitoring
  motor.useMonitoring(Serial);
  motor.monitor_downsample = 0; // disable monitor at first - optional
  ...

}
void loop(){
  ....

  ....
  // real-time monitoring calls
  motor.monitor();
  // real-time commander calls
  command.run();
}

I hope it helps, do not hesitate to let me know if problem persist.

Regards

I have the same question. This is my entire code for attempting to make a firmware that SimpleFOCStudio can talk to.

I have successfully spun my motor with angle_openloop.

I believe my problem might be that I don’t know what to fill-in for the parts of the code that are represented by the “. . . .” in the example code.

I will try adding more motor parameters in the setup().

Do I need to have anything more in the loop()? I figure that SimpleFOCStudio will be telling my motor what to do.

Thank you.

#include <SimpleFOC.h>

// My motor is a BDUAV-2204-260kv, a little gimbal motor.
// no encoder
// 7 pole pairs, 
// phase resistance = 10 ohms
BLDCMotor motor = BLDCMotor(7, 10);

// I use a SimpleFOCShield 2.0.4
// the phase pins are on pins 9, 5, and 6
// the enable pin is 8
BLDCDriver3PWM driver = BLDCDriver3PWM(9, 5, 6, 8);

Commander command = Commander(Serial);
void doMotor(char* cmd) { command.motor(&motor, cmd); }

void setup(){
  command.add('M', doMotor, "motor"); 
  motor.useMonitoring(Serial);
  motor.monitor_downsample = 0; 
}

void loop(){
  motor.monitor();
  command.run();
} 

Hi,

Can you try adding a

Serial.begin(115200);

at the start of your setup?

What happens is that I can upload the firmware, and the motor turns. Then I launch Studio from a conda env command line.

I can open the command line tab and connect there. I can see the serial log.

Connected ...
.0000	4.7418
2.0000	5.0000	0.0000	0.00	0.00	2.0000	4.8847
Disconnected ...
Connected ...
.0000	0.0000	0.00	0.00	2.0000	1.7826
2.0000	5.0000	0.0000	0.00	0.00	2.0000	1.9240
000	0.0000	0.00	0.00	2.0000	1.6399
2.0000	5.0000	0.0000	0.00	0.00	2.0000	1.7826
2.0000	5.0000	0.0000	0.00	0.00	2.0000	1.9240
MOT: Monitor enabled!
MOT: Init
MOT: Enable driver.
2.0000	5.0000	0.0000	0.00	0.00	2.0000	0.1325
2.0000	5.0000	0.0000	0.00	0.00	2.0000	0.2758
2.0000	5.0000	0.0000	0.00	0.00	2.0000	0.4164

But when I try the Tree View or Form View, there are no signs of life. The buttons can be clicked, and most of them react, but nothing changes with regards to the data on screen. And after I click a few things, the UI hangs.

Then I see something like this in the shell:

(simplefoc) mark@delbert:~/simplefoc/SimpleFOCStudio$ python3 simpleFOCStudio.py 
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
Traceback (most recent call last):
  File "/home/mark/simplefoc/SimpleFOCStudio/src/simpleFOCConnector.py", line 872, in run
    self.handle_received_data(reading.decode())
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe2 in position 5: invalid continuation byte
Segmentation fault (core dumped)
(simplefoc) mark@delbert:~/simplefoc/SimpleFOCStudio$

(simplefoc) mark@delbert:~/simplefoc/SimpleFOCStudio$ python3 -V
Python 3.6.13 :: Anaconda, Inc.
(simplefoc) mark@delbert:~/simplefoc/SimpleFOCStudio$ 

I’m running this on Linux Mint, in a miniconda3 environment.

Here’s my latest version of the code. I added Serial.begin(), and some other stuff.

#include <SimpleFOC.h>

// My motor is a BDUAV-2204-260kv, a little gimbal motor.
// no encoder
// 7 pole pairs, 
// phase resistance = 10 ohms
BLDCMotor motor = BLDCMotor(7, 10);

// I use a SimpleFOCShield 2.0.4
// the phase pins are on pins 9, 5, and 6
// the enable pin is 8
BLDCDriver3PWM driver = BLDCDriver3PWM(9, 5, 6, 8);

// InlineCurrentSensor constructor
//  - shunt_resistor  - shunt resistor value
//  - gain  - current-sense op-amp gain
//  - phA   - A phase adc pin
//  - phB   - B phase adc pin
//  - phC   - C phase adc pin (optional)
//InlineCurrentSense current_sense  = InlineCurrentSense(0.01, 50, A2, A0);

float target_velocity = 2; // rad/s

Commander command = Commander(Serial);
void doTarget(char* cmd) { command.scalar(&target_velocity, cmd); }
void doMotor(char* cmd) { command.motor(&motor, cmd); }

void setup(){
  Serial.begin(115200);
  driver.voltage_power_supply = 5;
  driver.init();
  // current_sense.init();
  motor.linkDriver(&driver);
  // motor.linkCurrentSense(&current_sense);
  motor.current_limit = 0.5;
  motor.velocity_limit = 20;
  motor.voltage_limit = 4;
  motor.monitor_variables = _MON_CURR_D| _MON_CURR_Q | _MON_VOLT_D | _MON_VOLT_Q | _MON_TARGET | _MON_VEL | _MON_ANGLE; 
  motor.monitor_downsample = 100; 
  motor.controller = MotionControlType::velocity_openloop;
  motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
  command.add('M', doMotor, "motor"); 
  command.add('T', doTarget, "target velocity");
  
  motor.useMonitoring(Serial);
  motor.init();
  //current_sense.gain_b *=-1;
  //current_sense.skip_align = true;
  //motor.initFOC();
  _delay(1000);
}

void loop(){
  motor.monitor();
  motor.move(target_velocity);
  //motor.loopFOC();
  command.run();
}

I have SimpleFOCStudio working now.
I switched from using the Arduino Mega2560 to using a Nucleo-F446RE.
And I bumped up the monitor_speed to 2000000.

I’m not sure why that helped, but at least I’m getting some response out of it now.

1 Like