# SimpleFOCStudio Base Code

**URL:** https://community.simplefoc.com/t/simplefocstudio-base-code/948
**Category:** developement
**Created:** [June 3, 2021, 5:06am UTC](https://community.simplefoc.com/t/simplefocstudio-base-code/948 "2021-06-03T05:06:23Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![MichaelC](https://community.simplefoc.com/user_avatar/community.simplefoc.com/michaelc/32/1446_2.png) [@MichaelC](https://community.simplefoc.com/u/MichaelC)
#### Post date: [June 3, 2021, 5:06am UTC](https://community.simplefoc.com/t/simplefocstudio-base-code/948/1 "2021-06-03T05:06:23Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![JorgeMaker](https://community.simplefoc.com/user_avatar/community.simplefoc.com/jorgemaker/32/546_2.png) [@JorgeMaker](https://community.simplefoc.com/u/JorgeMaker)
#### Post date: [June 3, 2021, 1:06pm UTC](https://community.simplefoc.com/t/simplefocstudio-base-code/948/2 "2021-06-03T13:06:03Z")

</div>

> [@MichaelC](#):
>
> Is there base micro controller code or an a example that does closed loop position control while

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 page](https://docs.simplefoc.com/studio) … **remember to add M to the GUI otherwise it will not work**

![image](https://community.simplefoc.com/uploads/default/original/2X/3/37da335be326c66e39a6fa565135b706d750e43c.png)

```auto
#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

---

<div class="post-metadata">

### Author: ![marked23](https://community.simplefoc.com/user_avatar/community.simplefoc.com/marked23/32/2656_2.png) [@marked23](https://community.simplefoc.com/u/marked23)
#### Post date: [March 20, 2022, 6:51pm UTC](https://community.simplefoc.com/t/simplefocstudio-base-code/948/3 "2022-03-20T18:51:27Z")

</div>

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.

```auto
#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();
} 

```

---

<div class="post-metadata">

### Author: ![runger](https://community.simplefoc.com/user_avatar/community.simplefoc.com/runger/32/395_2.png) [@runger](https://community.simplefoc.com/u/runger)
#### Post date: [March 21, 2022, 1:09pm UTC](https://community.simplefoc.com/t/simplefocstudio-base-code/948/4 "2022-03-21T13:09:34Z")

</div>

Hi,

Can you try adding a

`Serial.begin(115200);`

at the start of your setup?

---

<div class="post-metadata">

### Author: ![marked23](https://community.simplefoc.com/user_avatar/community.simplefoc.com/marked23/32/2656_2.png) [@marked23](https://community.simplefoc.com/u/marked23)
#### Post date: [March 22, 2022, 4:41am UTC](https://community.simplefoc.com/t/simplefocstudio-base-code/948/5 "2022-03-22T04:41:18Z")

</div>

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.

```plaintext
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:

```bash
(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.

```auto
#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();
}

```

---

<div class="post-metadata">

### Author: ![marked23](https://community.simplefoc.com/user_avatar/community.simplefoc.com/marked23/32/2656_2.png) [@marked23](https://community.simplefoc.com/u/marked23)
#### Post date: [March 24, 2022, 5:36am UTC](https://community.simplefoc.com/t/simplefocstudio-base-code/948/6 "2022-03-24T05:36:44Z")

</div>

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.
