SimpleFOC on Arduino Giga - BLDC Motor buzzing

What’s wrong by using mbed? If you follow mbed’s PWM code, it just ends at the same HAL functions as stm32duino does… only HardwareTimer is missing, however I don’t think this is needed.

I’ve got something compiled, but struggled at the (in my view) rather complex scoring logic, in which I don’t see the benefit for my use case.

There are three problems with it:

  1. we have a well working and full featured driver for STM32. Basing the Portenta driver on mbed would force us to maintain 2 seperate drivers for STM32 chips, while it looks very much like we could combine them into the existing driver. Having seperate drivers is the main reason IMHO the Portenta one is not full featured and doesn’t see much development.

  2. The mbed timer API (like the HardwareTimer one also) isn’t sufficient by itself to do everything we need so we wind up doing HAL and LL calls anyway. I don’t think mixing them makes the code easier to understand.

  3. In my personal experience comparing mbed to non-mbed performance you pay a heavy performance price for all those extra abstraction layers… that’s not desirable when doing motor control applications.

1 Like

I fully agree on the goal to have a common STM32 driver implementation. My question was meant to use your HAL only code also for the original Arduino-mbed core - the only dependency on stm32duino is IMHO the HardwareTimer abstraction layer.

For the moment I’m fine with my modified mbed-driver (I don’t use current sense and therefore I don’t need the PWMs aligned), there’s no significant performance difference since both implementations will end up at the same HAL functions. And for my application I’m using Arduino’s RPC library to communicate with the main core, so no additional effort here. :slight_smile:

Hey, that’s what my PR does - to eliminate the HardwareTimer dependency and add a few other things that mbed is missing. Along the way I made a few other improvements…

1 Like

I’m equally very busy, and this board wasn’t meant for motor control. Let’s play by ear.

Cheers,
Valentine

1 Like

I can only comment on Mbed for RP2040, but in that case both apps ran slower because of the shared memory stacks.
It’s probably easier to have two fully independent MCUs on board. If they only communicate via data polling, their apps run much smoother.

Got the new boards and now all 5 motors are working: :slight_smile:

3 Likes

That is very awesome. Will it be open source?

Awesome! That looks like a Giga board under there. I am assuming this is or will work with mbed??? I don’t know how all this works for open sourcing and releasing but just how does all that work? Can’t wait to get this all working!!!

Sure, I’ve posted the link and the minimalistic mbed-driver above.

It’s basically a copy of the STEVAL-GMBL02, but with two more drivers and some additional breakouts. Currently I don’t use current sensing and there’s only a common shunt per motor on the board.

1 Like

Hey for anyone interested, this PR is now a little bit tested and working:

I’ve tested it on Portenta H7 and the Giga R1 boards with 3-PWM so far. Results are encouraging.

If anyone wants to try it, you have to check out this branch of the library from github:

and then put it in your $HOME/Documents/Arduino/libraries/ folder (or equivalent for your OS), replacing any version that might be there already from the library-manager.
In PlatformIO you can put the github cloned library in your project’s lib folder and remove the library from the platformio.ini

Finding pins on the Portenta H7 or Giga R1 for PWM is a huge pain. Here are a few tips:

For example, on Giga R1 3-PWM can be done on pins 6, 8 and 9, which uses only timer TIM4.
I’ll post an example for 6-PWM once I’ve figured one out :sweat_smile:

I will continue my tests in the meantime, but if you do try it out, let us know how it goes.

2 Likes

Thanks a lot, I’ll give it a try and let you know!

Yeah, finding the right pins is a bit tricky, these are the ones I’m using for 3-PWM - be aware, that TIM2 and TIM5 are not usable on the Giga:

// https://github.com/arduino/mbed-os/blob/extrapatches-6.17.0/targets/TARGET_STM/TARGET_STM32H7/TARGET_STM32H747xI/TARGET_GIGA/PeripheralPins.c
// TIM5 cannot be used because already used by the us_ticker
// TIM2 cannot be used because already used by the us_ticker (DUAL_CORE)
const PinMap PinMap_PWM[] = {
  { PK_1,  PWM_1,  STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF1_TIM1,  1, 0)}, // TIM1_CH1 - 1.1
  { PA_9,  PWM_1,  STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF1_TIM1,  2, 0)}, // TIM1_CH2 - 1.2
  { PJ_9,  PWM_1,  STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF1_TIM1,  3, 0)}, // TIM1_CH3 - 1.3

  { PE_6,  PWM_15, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF4_TIM15, 2, 0)}, // TIM15_CH2 - 2.1
  { PA_6,  PWM_13, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF9_TIM13, 1, 0)}, // TIM13_CH1 - 2.2
  { PA_2,  PWM_15, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF4_TIM15, 1, 0)}, // TIM15_CH1 - 2.3

  { PB_4,  PWM_3,  STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF2_TIM3,  1, 0)}, // TIM3_CH1 - 3.1
  { PA_7,  PWM_3,  STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF2_TIM3,  2, 0)}, // TIM3_CH2 - 3.2
  { PB_0,  PWM_3,  STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF2_TIM3,  3, 0)}, // TIM3_CH3 - 3.3

  { PD_13, PWM_4,  STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF2_TIM4,  2, 0)}, // TIM4_CH2 - 4.1
  { PB_8,  PWM_4,  STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF2_TIM4,  3, 0)}, // TIM4_CH3 - 4.2
  { PB_9,  PWM_4,  STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF2_TIM4,  4, 0)}, // TIM4_CH4 - 4.3

  { PJ_8,  PWM_8,  STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF3_TIM8,  1, 0)}, // TIM8_CH1 - 5.1
  { PC_7,  PWM_8,  STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF3_TIM8,  2, 0)}, // TIM8_CH2 - 5.2
  { PK_0,  PWM_8,  STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF3_TIM8,  3, 0)}, // TIM8_CH3 - 5.3
};

Great! thank you…Sorry but I am not familiar with this system and can’t find the files. Can you please be more specific as to how to find the files as the link provided seem to point to the general top of a folder tree. What files specifically need to be replaced and where are they located in the folder tree. Also, I don’t see any revision numbers in the files themselves. How do you identity what revision a file is and what revision are we looking for the files with this update?

Thanks again for the help and am looking forward to trying the code…

Hey @dro178 ,

I’m assuming you are using ArduinoIDE… I’m not sure which operating system you use, you’ll have to adapt the paths and commands a bit to match your os.

You need a local copy of the SimpleFOC library from my branch. You can use your favourite git client UI, or just use git from the command line:

git clone https://github.com/runger1101001/Arduino-FOC.git
cd Arduino-FOC
git checkout stm32_hal_pwm

this will check out my fork of SimpleFOC and switch to my “stm32_hal_pwm” branch which the PR is based on.

Then find your Arduino libraries folder. For me it is in $HOME/Documents/Arduino/libraries/
In this folder you will probably find an existing version of SimpleFOC library - there will already be a folder “Arduino-FOC”. Move the whole existing folder away, either to the trash or keep it somewhere else if you plan to put it back.

Then either copy the Arduino-FOC folder created by git clone into the libraries folder, or create a symlink (Windows: directory junction) to it in the libraries folder.

When you start ArduinoIDE, you will be able to compile your code against the github version of the library. If you hunt for it, you will see something like this in the output in ArduinoIDE:

Using library Simple FOC at version 2.3.4 in folder: /Users/runger/git/Arduino-FOC 
Using library SPI in folder: /Users/runger/Library/Arduino15/packages/arduino/hardware/mbed_giga/4.1.5/libraries/SPI (legacy)
Using library Wire in folder: /Users/runger/Library/Arduino15/packages/arduino/hardware/mbed_giga/4.1.5/libraries/Wire (legacy)

Thanks for the tips. Whereas I have used github many times to download code, I have never used it as a repository manager. It took some doing, but I was able to download the stm32_hal_pwm branch (still not comfortable with it). The Arduino library manager gave me some fits as the normal SimpleFOC library installer uses “Simple_FOC” as the root and your download uses “Arduino-FOC”. The library manager did not like renaming this and I had to copy the updated files into the existing Simple_FOC folder. It compiled and uploaded but it did not drive the motor. I was using pins pwma-5, pwmb-9, and pwmc-6, enable-8. I saw that you recommended using 6,8,9 for the pwms on the giga to all use Timer-4. The board only allows pins 3 or 5 for pwma, neither of which are on timer-4. Is the requirement that all the pwms are on the same timer? Can I solder a wire-wrap wire to a different pin on the board to the pwm pin selection pad to make a different selection like pin 8 for pwma as you suggest? I am assuming that the enable can be also jumpered to any digital pin if needed. Getting all the pwms on the same timer is an issue with the limited solder jumper options on the SimpleFOC board. I have gone back and forth to the old version to be sure that things still are working hadwarewise, albeit quite noisy.

Update…I got the new drivers working on the Giga M4! To get all the PWMs on the same timer (TIM4) as recommended, I used PWMA-pin 8, PWMB-pin 9, PWMC-pin 6, enable-pin 7. Because the board jumper options only allow for pins 3 or 5 for PWMA, I put a wire wrap jumper from the PWMA pad to the board pin 8. I set the PWM frequency to 10kHz and all the noise went away and ran fast and smooth. My application is for position control. More testing tomorrow. What PWM frequency should I be using? Thank you all!

2 Likes

Great news!

Yes, unfortunately the Giga’s pinout with the timers does not allow for direct use of the SimpleFOC shield :frowning: very sad, but I’d say based on their choice of pin assignment that they did not have motor control in mind,

This depends on driver and motor, and your appetite for audible noise, but the default for STM32 is 25kHz. Because the Giga is so fast, you could set it much higher even if your hardware supports it. For the SimpleFOC shield I think 25kHz will work well.

I have been using the fixed new drivers for a few weeks now, and it works great! I will use this to keep my application development going as it works great to have the FOC running on the M4 processor and all my control processing in the M7. I was wondering if this was going to get a formal release? What is the plan for this going forward. I’d like to recommend this Giga/SimpleFOC configuration to others if there is a public release. Just looking and planning down the road…

Sorry…forgot to mention, Thanks again for the support on this issue!

Also, does anyone know of a USA supplier of the SimpleFOC shields? All I can find are foreign suppliers and most of them are out of stock. I have had 5 shields on order at Makerbase3D since July 8th. They keep telling me “two more weeks”.

Ive seen that before… watch out!

cheers,
valentine