-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Torque mode, FOC? #11
Comments
Hi, Your nickname rings a bell. Are you in the simpleFOC community? If you want to port FOC, check this, someone had started to port it, I did small adjustments but couldn't finish as I was busy with something else. Another idea I had is This |
Oh yes, https://simplefoc.com/ looks very promising. Not only the stm32f1 family but also the stm32f4 family is said to be supported: https://docs.simplefoc.com/stm32_mcu But to my understanding, phase current sensing is only needed without hall sensors. But Candas said the EFeru FOC code needs two phase currents (the third can be derived from the first two). The docs of SimpleFOC in https://docs.simplefoc.com/code says:
That example code looks really nice and it should be straight forward to get two motors (without phase current sensing) running on the old stm32f103 and the new stm32f403 boards !? There is https://github.com/keyboardio/ArduinoCore-GD32-Keyboardio but maybe only the GD32F303 is supported and not the GD32F130 that is found on these Gen2 boards. Adding FOC to my Gen2.x repo is not really on my todo list.
So additional structs Server2HoverTorque or Server2HoverRpm might be in reach. So yes when i have finished a closed loop for positioning a rotation angle, it would be easy to add a torque mode. @Candas1 i have probed the DIO2032 dual opamp outputs of the gen2.2 board with my dso and they do not seem to be current and voltage: Do you recognize these outputs (motor was spinning) as tow low-side phase currents ? Greetings from sunny Germany :-) |
No simplefoc cannot handle 2 motors yet. |
Thanks Candas1 for your feedback! We have to find such a solution anyway for the modern stm32f403 boards anyway ? |
Also: the "split boards style" hoverboards all have a processor and drive only one motor, communicating with each other over serial. So, all those are great candidates for Simplefoc. It's the first generation boards that used a single processor driving both motors (and using side boards for the IMUs). Thanks for the additional links @Candas1 (and, yes, it's me on SimpleFOC forums :). I'll have a look at them, especially the Arduino port, But the GD code (https://github.com/CommunityGD32Cores/ArduinoCore-GD32) is still pretty untested, so I will need to get a GD32F130 chip, replace it on a Bluepill, and verify that PWM, ADC and timers all work as expected before trying it on a board where the wrong signal can result in magic smoke release from the MOSFETs. The split board hoverboards are a near perfect source of high power drivers: I just got a 25V/250W hoverboard with a dead battery for $10. Basically $2.5 each for drivers and motors :) Problem is, they all use different processors and it's a crapshoot to know what you will find when you open one, so a generic framework is the best option. And I'm ok using encoders instead of the Hall motors, if that makes SimpleFOC easier to run on them |
There is even a i2ccommander that could be used to control more than 2 boards. Again I am not saying it's easy, this will take time. But if we get there we would have made gd32-arduino and simplefoc better, and we can go further than EFeru's FOC as simplefoc has also position control, it's more maintainable and has more support. Last few months I struggled improving EFeru's FOC because of the Matlab model. The quickest solution if you only care about FOC and torque would be the link I shared with EFeru's foc. But you need to find a board that has the current sensing. If you want to go fast, go alone. If you want to go far, go together |
And the work recently done by @RoboDurden to document the different board layout is very useful, we could use those different define.h files |
Actually one of the improvements I did on mainboards that I didn't share yet is separating the current sensing and FOC for each motor. But again mainboards would need even more work on the simpleFOC side, so I would focus on splitboards for now. The extrapolation is not a problem in normal conditions, but you need to take care of transient cases like when direction changes, EFeru's does this well. |
Okay i better cancel my uart chain protocol and start with simpleFOC :-) i switched to the available genericGD32F130R8 using the project environment switcher in the bottom taskbar. The R8 has the same hardware as the C8 but offers more io pins in the 64 pin package. Choosing an led pin from one of my defines2-x
I will try to upload the bin file later the day. But i am totally new to PIO (platformIO). |
You can also check what I did here |
@robcazzaro if you want to join the gd32 coding and live inside Europe I would be happy to ship you two sideboards with a gd32f130c6 for free :-) |
Okay i am able to flash with PlatformIO. The flash header in the image of EFeru was wrong for me: My header needed GND , DIO , CLK , 3.3V I powered the board with 14V but no led blinking. @Candas1 did you succeed with your https://github.com/Candas1/Sideboard-Arduino and the firmware is running and can be debugged ? |
YES, the Candas1 Sideboad code works. At least i can debug step by step :-) |
Nice collection of sideboards you have, @RoboDurden 😊 I live in the USA, probably shipping cost from Europe would be too much, given the size of those boards. Ordering a couple of Bluepills and GD32F130 from Aliexpress would be around $10, with delivery in <15 days. I have all the necessary tools to swap a processor easily and have done some very fine SMD soldering (0201, BGA). And, now that I think of it, I could get the different GC32 processors used in the split boards, so I could easily test various low level stuff safely. I need to check the HW for the various split boards and ensure I get all the processors used, just in case. But if you don't mind checking the cost of shipping to Seattle, WA, I'd appreciate if you could let me know the cost, though (I'd pay for shipping) Definitely happy to cooperate, sounds like a very worthwhile project. Caveat: I'm really good at the low level stuff (debugging, hw registers, timing, etc) and I have a collection of necessary tools (oscilloscope, logic analyzer, etc), not as good at the high level object oriented stuff. |
I took some time to look at the various layouts (I have a layout 2 board), and they all seem to use the GD32F130. Most seem to use the GD32F130C8T6, one uses the GD32F130K8, and if I see correctly the layout 4 uses a GD32F130C6T6. So only the F130 family needs to be supported. @RoboDurden can you please confirm the above? C8 is the most complete one, C6 has half the FLASH and half the RAM (32k and 4k), one less 16bit timer, only one I2C and SPI buses. The K8 is the same as the C8, obviously with fewer pins than the C8 (32 vs 48). We need to pay attention to the memory optimizations, it's easy to run out of FLASH or RAM if the Arduino framework doesn't do a good job (on the C6 only, C8 is not a concern). Based on the above, I'd get a C8 and a C6 processor to install on a Bluepill, as to be able to test every HW variation. I could get a K8 adapter, but the C8 and K8 are the same internally, so I doubt that the pin routing will cause issues. Having a Bluepill would help troubleshoot HW issues (isolate the processor from the rest of the HW), methinks |
There are some STM32f103 also |
Good point 😊. But for those a normal Bluepill suffice, and in any case the Arduino support is fully working. Definitely something to keep in mind, but I was focusing on GD32 HW and worried about the quality of the GD32 Arduino port for things like injected mode ADC, timers, DMA, etc |
And yess, finally i can code object oriented on a GD32. These changes in Candas1 main.cpp do work:
@robcazzaro shipping to Seattle will simply take too long for joining the party now. I rather donate $5 via paypal to you so you buy some GD32F130 sideboards on used items platform. |
You mean you have a set of gen2.2 and gen2.4 ? gen2.2 has a K8 on board, gen2.3 and gen2.4 a C8. Maybe the 2.1 layout only has a C6. But this does not really matter. We just have to ensure not to use the additonal hardware of the better MCUs. I do not see much value in your bluepill approach. |
Okay i added the simpleFOC library via https://docs.simplefoc.com/library_platformio
But before going further with simpleFOC i should test the bascics the library needs. Timer, interrupts, analogRead i guess. |
I was reading this thread again It could be more work for pwm and adc |
Sure but I should find all the gd32 init code needed in my gen2.x repo |
I have a "layout 2.0" board, the one without gate drivers. So it's not safe for development for an unproven core like the GD32 one. And I have no way to buy a sideboard. I could try to look for used hoverboards locally, but most sell in the $75-$150 range and it's a crapshoot on what I will find in those. I have bought 2 already, one uses split boards with MM32SPIN05PFOP, which is a really different processor, and one has the layout 2.0 split boards. And I'd rather not kill those 😊 So I was thinking that, if I want to help, my best option is to have a Bluepill with the right processor. Also, in my experience, when chasing problems (like ADC sampling noise), it's convenient to have a known good reference board to identify problems due to the processor quirks vs the board components itself. Otherwise I can wait until you have a working implementation on the layout 2.0, and at that point use my split boards as development platforms. Any other suggestion? |
Okay @robcazzaro it might indeed be more fruitful if we each follow a siglthy different route. So you with a bare gd32 might test and solve problems that I can not. |
In the past I looked at the STM32duino implementation. It relies very heavily on the STM32 HAL, to the point of adding al ot of problems and workarounds for those problems. And SimpleFOC really builds on top of that abstraction, too. So I expect problems with more complex aspects like injected ADC sampling and timers. I might well be wrong, but I'd be surprised if you don't run into some unexpected behavior due to the differences between the GD32F130 and STM32F103 (to be clear: not doubting your skills). The GD32F103 is pretty much directly compatible, but not the F130. That's where I think that a dev board might be necessary and a good backup to have if anything goes wrong on your side |
I worked on injected adc for stm32f1, I should be able to help on gd32f130. |
Weird, I saw that in the spl drivers Hoverboard-Firmware-Hack-Gen2.x/HoverBoardGigaDevice/RTE/Device/GD32F130C6/gd32f1x0_adc.c Line 374 in c350d80
|
@RoboDurden the Serial port initialization happens here and the pin assignment here You might want to check if the pin numbers you use (PB6 PB7) are properly mapped to the actual GD32 GPIOs. This is something I can't check without an actual board, but according to this file the pin names seems to be PORTB_6 and PORTB_7. Analog pins seems to use the PA0, PA7, etc convention, but not the digital pins All the files I mentioned are under C:\Users\ [yourname] .platformio\packages\framework-arduinogd32 |
So i finally got the Serial1 to work on the rx/tx pins PB7/PB6 by directly overwriting in the generic variant.h (https://github.com/CommunityGD32Cores/ArduinoCore-GD32/blob/main/variants/GD32F130C8_GENERIC/variant.h#L133)
It is not sufficient to define at the beginning of the user code in main.cpp
Even so VisualCode (VC) greys the define lines in variant.h because they are already defined. But then i do not see a way for user code to set uart (or i2c) port to different pins without hacking the Arduino-Core. I again will try to define a new serial port with The Esp32 Arduino-Core is better in letting the pins be assigned in the Don't know what i should ask the GD32-Arduino-Core community for :-/ |
Okay, this works to replace Serial1: |
@RoboDurden have you tried with PORTB_6 and PORTB_7 instead of PB6, PB7? The code properly uses pin names to initialize the port, so it's just a matter of finding the right naming convention. If you don't want to bother digging into the lower levels of the GD32 Arduino code, don't worry: just hardcode things for now and overwrite the files. As soon as I have a working board, I will figure out the Serial code. The conversion between pin names and GPIO ports is pretty messy in the Arduino frameworks, and even in the SimpleFOC code you need to use weird tricks sometimes to ensure that the pins are mapper properly. It's something I have done before and have a quick way to address it I'd suggest not ask the GD32 folks for now, let's use them for things we can't figure out between the 3 of us |
Exactly! That is why i quickly added the oneliner that |
OK then check those option bytes is mandatory. I had already added this link in the readme. |
No I don't think so, TX was working fine, and RX failing only after a minute. But when I am back I will check what is the impact of each change. |
@robcazzaro @RoboDurden |
Well this issue has transformed into a nice chat group. |
Good point @Candas1 and apologies if I made things worse. I tend to get sidetracked a lot 😊 I think we should choose a repository, and create new issues for everything we discover (i.e. things like Serial initialization, specific code ports, etc) and keep the discussion on every issue focused on a single topic. That way it's easy to go back and find stuff and track progress by closing issues I'll leave it to you to decide where to have the repository and do the initial check-ins (unless we want to use https://github.com/Candas1/Sideboard-Arduino) |
I am not blaming you I am only suggesting. @RoboDurden had already creating one issue actually. But it's also true that you might not be aware of all the new issues |
slow i2c progress: https://github.com/RoboDurden/Split_Hoverboard_SimpleFOC/blob/main/src/main.cpp gd32-slave receiving string data from esp32_s2_mini-master is working:
Log output of https://github.com/RoboDurden/Split_Hoverboard_SimpleFOC/blob/main/arduino%20examples/ESP32_Mini_S2_I2c_Uart/ESP32_Mini_S2_I2c_Uart.ino#L52 :
00:48 here in Germany, good night :-/ |
Okay @Candas1 i had to fix the i2c_slave_tx code in the GD-Arduino-Core but now i have a working ESP32_master <-> GD32F130_slave GD32_Arduino_Core: ArduinoCore-GD32/issues/99 |
Oh wow, this is great news. |
@Candas1 , My fix is for using a GD32 as a slave to be controlled by a master like the i2cCommander use case. Now i do not really like i2cCommander any longer as it is very old school in writing/reading single registers. With my nice little helper functions |
Congratulations for your first PR 😜 |
Hi guys, I am back from my unplanned leaves, but I have planned leaves end of next week again. When 6pwm is working, we could already achieve trapezoidal control, which is what your firmware does now. So it's a long journey, but each step will add value. @RoboDurden , regarding your comments about how i2ccommander is coded. The reason I feel this new Arduino + simpleFOC approach is exciting is because I already see I can develop much faster using libraries. I saw there are libraries for battery management, handling buttons, ibus and what not. When contributing to Eferu's FOC firmware, I had to code everything myself, I for example coded a poor version of commander, which is good for learning but takes a lot of time. Now I would just use the commander from simpleFOC. My aim now is to get arduino-gd32 and simplefoc working on the splitboards, for any developer to be able to code his firmware. |
At the moment i have a 2.0 and a 2.5 test setup. According to your spread sheet https://docs.google.com/spreadsheets/d/15msbDAIMxC2rIkq8Au8vf82ub1qEaS67Lc1cFb86Jpc/edit#gid=0 the 2.0 has two low-side phase currents and the 2.5 even alle 3 low-side. But i have not tested either of these pins yet. I guess best chances to find a gen2.0 board is to buy an old 25V 7s hoverboard with these bldc motors that have a Mercedes like star on the sides. Yes i also wanted to add a "i2ccommander" to my gen2.x repo but did not want to code i2c on that stupid low level. It would be nice for me to have a drivers/hardware_specific/gd32/gd32_mcu.cpp that compiles successfully because these empty functions from the generic file cover all the missing functionality. |
I have all possible splitboards in boxes somewhere, I just need to find it. ok I will start with a gen 2.0 then. Once I see it's not harming the hardware I will give you a heads up. |
HnVideoEditor_2023_06_14_172056522.mp4 |
Hallelujah, that looks like you already get the motor spinning :-) |
I just forked your repository to save time. Including this to my repository now would be a mess. I just gave a fixed target for my tests, haven't used usart yet. I never got something working so easily lol, it worked at the first attempt. I will do more checks and commit later today so you can see what I changed. |
I committed my code here but please be really careful. This sketch is already using 91% of the flash, the GD32F130C6T6 have really little memory. The I2C communication you were coding might fit. |
@Candas1 yes, I do have quite a lot of experience optimizing code for size, and I'm sure that especially at the beginning it would be easy to find savings. Later might be hard to find further optimizations I just tried compiling your project (platformio.ini was missing the SimpleFOC library reference, btw), and Ï see 63.7% RAM used, 86.6% FLASH used Bad news, the most basic step (using -Os) is already done, so I will need to dig into the elf to understand where the space is used. And from a quick look at the code, elf and linker files, there's nothing obviously easy to optimize, the only thing that caught my eye is the size of the .rodata section (2400 bytes), which seems too big. The most obvious easy candidate is to remove all the DEBUG prints which are not needed once the code runs. The debug code is supposed to be disabled using -D SIMPLEFOC_DISABLE_DEBUG, but that code ir broken. If I add that option, the compilation aborts: the code expects the debug prints to be in a macro, but the code uses SimpleFOCDebug:: class directly. I hacked a way to disable debug print, and I go from 86.6% down to 83.3%. It's a start I'll open an issue on disabling the debug print in SimpleFOC (EDIT simplefoc/Arduino-FOC#279) Do you mind enabling issues in your repository, so I can start creating one for the memory size, as a work item and to discuss tradeoffs |
Thanks a lot! I tried LTO but it's always breaking the program for me. The look up tables for sin and cos also use quite some space. I am wondering if generating those tables in ram at startup would save any space. We could use libraries like qfplib that are supposed to be smaller and faster. |
@RoboDurden Do you know if the emergency stop is really connected on this layout? If yes it will act as an overcurrent trigger and stop the pwm output. It would only be a matter of enabling This with the right polarity and would be safer. [EDIT] And probably setting PB12 properly |
Just one thought. So don't put a target too high for now. |
Just wondering if you are planning to add FOC and torque mode like the Eferu project, or if you know of any FOC algorithm implemented for the GD32F130C8T6.
If not, I might want to try "merging" your base GD32 code with the FOC algorithm from Eferu
The text was updated successfully, but these errors were encountered: