{"id":459,"date":"2024-04-10T22:00:27","date_gmt":"2024-04-10T22:00:27","guid":{"rendered":"https:\/\/www.hardwarehacker.co.uk\/blog\/?p=459"},"modified":"2024-04-14T19:45:17","modified_gmt":"2024-04-14T19:45:17","slug":"bldc-controller-phase-6-productisation","status":"publish","type":"post","link":"https:\/\/www.hardwarehacker.co.uk\/blog\/bldc-controller-phase-6-productisation\/","title":{"rendered":"BLDC Controller phase 6: productisation"},"content":{"rendered":"\n<p>In the <a href=\"https:\/\/www.hardwarehacker.co.uk\/blog\/bldc-controller-phase-5-control\/\">last post<\/a>, I&#8217;d just got a basic control loop going to control the speed of the motor by varying the throttle.  In this post I&#8217;ll try to summarise what it took to get from there to something we could use in the robot.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2024\/04\/image.png\"><img loading=\"lazy\" decoding=\"async\" width=\"992\" height=\"747\" src=\"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2024\/04\/image.png\" alt=\"\" class=\"wp-image-460\" srcset=\"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2024\/04\/image.png 992w, https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2024\/04\/image-300x226.png 300w, https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2024\/04\/image-768x578.png 768w\" sizes=\"(max-width: 992px) 100vw, 992px\" \/><\/a><figcaption class=\"wp-element-caption\">Bench test with 4 motors<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Modularisation<\/h2>\n\n\n\n<p>Up to now, I&#8217;d built the code for controlling the motor in the &#8220;main loop&#8221; of the program, but I had the goal to control multiple motors from one Pico.  To achieve that I refactored the code so that the state variables for each motor were contained in a struct, and I moved the inline code into functions that take a pointer to that struct as first argument (this is a common way of doing &#8220;object oriented&#8221; programming in C).  This gave an API that looked like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void motor_init(struct motor_cb *cb, uint pin_a, uint pin_b, uint pin_C, uint pin_pwm_in);\nvoid motor_calibrate(struct motor_cb *cb);\nuint16_t motor_get_calibration(struct motor_cb *cb);\n...<\/code><\/pre>\n\n\n\n<p>With that in hand, I could now have four copies of the struct, each configured on different pins.  The main loop then just needs to call each motor&#8217;s control functions in series to let them each update the state:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>...\nfor (int m_upd_idx = 0; m_upd_idx&lt;4; m_upd_idx++) {\n    motor_update(&amp;m&#91;m_upd_idx]);\n}\n...<\/code><\/pre>\n\n\n\n<p>Of course, one the code was modularised there was a bunch more tuning to do and I adjusted the control algorithm to use a proper PID control.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Add I2C control<\/h2>\n\n\n\n<p>A motor driver isn&#8217;t much use if you can&#8217;t control it so I used the Pico&#8217;s I2C peripheral capability to make it configurable from the main Raspberry Pi.  I started with the excellent example from the Pico examples repo and customised it to write 16-bit registers instead of random access.<\/p>\n\n\n\n<p>At time of writing the Pico now has over a dozen registers:<\/p>\n\n\n\n<ul>\n<li>An overall control register to enable\/disable the motors, trigger calibration, etc.<\/li>\n\n\n\n<li>A watchdog timeout register<\/li>\n\n\n\n<li>Speed control x4<\/li>\n\n\n\n<li>Calibration x4<\/li>\n\n\n\n<li>Distance travelled x4<\/li>\n\n\n\n<li>Temperature<\/li>\n\n\n\n<li>Current<\/li>\n\n\n\n<li>Voltage<\/li>\n\n\n\n<li>Power<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Watchdog<\/h2>\n\n\n\n<p>From past experience, I have found that the main Raspberry Pi will crash\/reboot\/get powered down unexpectedly.  It is &#8220;really bad&#8221; if the motors keep going at full tilt when that happens.  To deal with that, I added a watchdog timer; if no commands are received on the I2C bus for a configurable timeout, the Pico shuts down the motors.  The main Pi has to &#8220;pet the watchdog&#8221; a few times a second to stop it from timing out.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">DMA<\/h2>\n\n\n\n<p>To improve performance, I made use of the RP2040&#8217;s DMA controller to shuttle sensor readings from the PIO block to main memory without CPU interaction.  I was also able to chain a second DMA to timestamp each sensor reading, which helped a lot with accuracy when runing multiple motors at once. It&#8217;s useful to know if the sensor reading is actually from half a millisecond ago.  Interrupts were another option but DMA has even lower overhead since it happens without the CPU&#8217;s involvement.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">INA219<\/h2>\n\n\n\n<p>To monitor battery and motor health, I used a second I2C block to connect to an INA219 voltage\/current monitor.  This is then exposed over I2C.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Temperature<\/h2>\n\n\n\n<p>The Pico has a convenient internal temperature probe, I made that available on the I2C bus for monitoring.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wiring it up<\/h2>\n\n\n\n<p>This turned out to be a real challenge.  There is a lot to fit into a tiny space:<\/p>\n\n\n\n<ul>\n<li>4 motor driver boards<\/li>\n\n\n\n<li>The pico<\/li>\n\n\n\n<li>Wiring for the 12 PWM signals<\/li>\n\n\n\n<li>12 motor outputs<\/li>\n\n\n\n<li>Power (both motor and pico)<\/li>\n\n\n\n<li>I2C<\/li>\n\n\n\n<li>4 sensors, each with 3 wires<\/li>\n<\/ul>\n\n\n\n<p>I designed a 3D printed module to tame it but it took a lot of careful soldering to squeeze it all in.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2024\/04\/image-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"562\" height=\"747\" src=\"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2024\/04\/image-1.png\" alt=\"\" class=\"wp-image-461\" srcset=\"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2024\/04\/image-1.png 562w, https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2024\/04\/image-1-226x300.png 226w\" sizes=\"(max-width: 562px) 100vw, 562px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2024\/04\/image-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"562\" height=\"747\" src=\"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2024\/04\/image-2.png\" alt=\"\" class=\"wp-image-462\" srcset=\"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2024\/04\/image-2.png 562w, https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2024\/04\/image-2-226x300.png 226w\" sizes=\"(max-width: 562px) 100vw, 562px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">A minor disaster<\/h2>\n\n\n\n<p>Of course, it didn&#8217;t work on the first try; my soldering iron was playing up and I overheated one of the motor driver boards, blowing out a trace.  Thankfully I was able to track it down and apply a bodge wire:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2024\/04\/image-3.png\"><img loading=\"lazy\" decoding=\"async\" width=\"992\" height=\"747\" src=\"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2024\/04\/image-3.png\" alt=\"\" class=\"wp-image-463\" srcset=\"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2024\/04\/image-3.png 992w, https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2024\/04\/image-3-300x226.png 300w, https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2024\/04\/image-3-768x578.png 768w\" sizes=\"(max-width: 992px) 100vw, 992px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>It felt pretty good to hold the finished and working module in my hand after several months of work and it&#8217;s working great in the bot (with a bit of tuning still to do to account for the momentum of the bot).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the last post, I&#8217;d just got a basic control loop going to control the speed of the motor by varying the throttle. In this post I&#8217;ll try to summarise what it took to get from there to something we could use in the robot. Modularisation Up to now, I&#8217;d built the code for controlling &hellip; <a href=\"https:\/\/www.hardwarehacker.co.uk\/blog\/bldc-controller-phase-6-productisation\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">BLDC Controller phase 6: productisation<\/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],"tags":[20,17,22,2,16,21],"_links":{"self":[{"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/posts\/459"}],"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=459"}],"version-history":[{"count":3,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/posts\/459\/revisions"}],"predecessor-version":[{"id":466,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/posts\/459\/revisions\/466"}],"wp:attachment":[{"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}