{"id":398,"date":"2024-02-04T19:25:04","date_gmt":"2024-02-04T19:25:04","guid":{"rendered":"https:\/\/www.hardwarehacker.co.uk\/blog\/?p=398"},"modified":"2024-03-14T17:53:27","modified_gmt":"2024-03-14T17:53:27","slug":"bldc-controller-phase-4-moving-to-c","status":"publish","type":"post","link":"https:\/\/www.hardwarehacker.co.uk\/blog\/bldc-controller-phase-4-moving-to-c\/","title":{"rendered":"BLDC controller phase 4: moving to C"},"content":{"rendered":"\n<p>While I had <a href=\"https:\/\/www.hardwarehacker.co.uk\/blog\/bldc-motor-phase-3-finding-the-limits-of-micropython\/\">got a motor moving in MicroPython<\/a>, I found I hit a wall with the garbage collector causing multi-millisecond pauses. <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2024\/02\/image-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"999\" height=\"752\" src=\"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2024\/02\/image-1.png\" alt=\"\" class=\"wp-image-400\" srcset=\"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2024\/02\/image-1.png 999w, https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2024\/02\/image-1-300x226.png 300w, https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2024\/02\/image-1-768x578.png 768w\" sizes=\"(max-width: 999px) 100vw, 999px\" \/><\/a><figcaption class=\"wp-element-caption\">Green signal high showing a multi-millisecond pause.  Multiple PWM  pulses go by on the yellow signal while Python is snoozing!<\/figcaption><\/figure>\n\n\n\n<p> A BLDC controller needs to be updating its model at at least 1KHz (and preferably more) so a multi-ms pause at random times is out of the question.  I tried a few approaches to avoid GC but, even using the &#8220;compile-to-machine code&#8221; options in MicroPython, I couldn&#8217;t seem to control it.  Beyond that, you&#8217;re only one small mistake from reintroducing GC by allocating something on the heap accidentally&#8230;. If I&#8217;m going to be one small mistake away from blowing my foot off, I&#8217;d rather work in C, where that&#8217;s perfectly normal \ud83d\ude42<\/p>\n\n\n\n<p>Porting the code to C went very smoothly:<\/p>\n\n\n\n<ul>\n<li>The Pico C SDK is wonderful, with excellent documentation.  Probably the smoothest C SDK bring-up that I&#8217;ve ever experienced.  Along with copious examples, there&#8217;s even a GUI tool to generate a skeleton project that has a hello-world for each peripheral that you intend on using.<\/li>\n\n\n\n<li>I translated the PIO programs from Python syntax to native PIO assembler and the C SDK made it easy to build that into the project.<\/li>\n\n\n\n<li>Translating the driver code itself, I did a fairly straight port for the initial version, but I opted to  roll my own fixed point arithmetic instead of defaulting to floating point.  For example, rather than converting a raw PWM value (which ranges from 0-2<sup>12<\/sup>-1) I stuck with 2<sup>12<\/sup> as my base for angles.  That means that clamping to 0-360 degrees can be handled by bitwise AND with 2<sup>12<\/sup>-1 rather than an expensive modulo operation.<\/li>\n<\/ul>\n\n\n\n<p>And what was the end result?  The C code was significantly faster.  So fast that my trick of toggling a pin while the code was actively running didn&#8217;t seem to be working at first.  I had to zoom in on the oscilloscope to catch it!<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2024\/02\/image.png\"><img loading=\"lazy\" decoding=\"async\" width=\"999\" height=\"752\" src=\"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2024\/02\/image.png\" alt=\"\" class=\"wp-image-399\" srcset=\"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2024\/02\/image.png 999w, https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2024\/02\/image-300x226.png 300w, https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2024\/02\/image-768x578.png 768w\" sizes=\"(max-width: 999px) 100vw, 999px\" \/><\/a><figcaption class=\"wp-element-caption\">Equivalent C code, blink and you&#8217;ll miss it!<\/figcaption><\/figure>\n\n\n\n<p>Once I started work on the C code, I created a Github repo, the initial commit of the C code is <a href=\"https:\/\/github.com\/fasaxc\/pico-bldc\/commit\/3e386f9e8891bfafd6bfa71507e2951a585855eb\">here<\/a>.  The initial commit was still a prototype, it <\/p>\n\n\n\n<ul>\n<li>Measured the angle of the motor.<\/li>\n\n\n\n<li>Multiplied up to get the angle relative to the magnetic poles in the motor.<\/li>\n\n\n\n<li>Added a offset, controlled by a pair of pushbuttons.<\/li>\n\n\n\n<li>Drove the motor PWMs with a sine wave at the offset phase.<\/li>\n<\/ul>\n\n\n\n<p>Still, it was enough to prove that C was fast and that the motor could be driven efficiently by correctly controlling that phase angle.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While I had got a motor moving in MicroPython, I found I hit a wall with the garbage collector causing multi-millisecond pauses. A BLDC controller needs to be updating its model at at least 1KHz (and preferably more) so a multi-ms pause at random times is out of the question. I tried a few approaches &hellip; <a href=\"https:\/\/www.hardwarehacker.co.uk\/blog\/bldc-controller-phase-4-moving-to-c\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">BLDC controller phase 4: moving to C<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,9,1],"tags":[20,17,22,19,2,16,21],"_links":{"self":[{"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/posts\/398"}],"collection":[{"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=398"}],"version-history":[{"count":2,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/posts\/398\/revisions"}],"predecessor-version":[{"id":402,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/posts\/398\/revisions\/402"}],"wp:attachment":[{"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}