In my latest post, I wrote about programming the Clicker2 for PIC32MX using MPIDE, which is an Arduino-like IDE. Now I make one step forward and use this Cliker2 board as the “brain” of the MikroEketronika Buggy.
As with many other MikroElektronika solutions, the hardware of this project is very easy to implement. Just stick the USB UART into socket #1 of the Clicker2 for PIC32MX board, then install the Clicker 2 in the Buggy. There is only one single issue to overcome, but that’s no big deal: as the USB UART works only in socket#1 of the Clikcker2 board, the USB cable is obstructed by the raiser card for the mikroBUS #3 of the Buggy.
One might wish to install longer pins on the Clicker 2 for PIC32MX and on the USB UART to gain some extra height. If you can find one, a 90 deg USB cable can also avoid this issue.
Programming the Buggy in MPIDE
Now we come to the fun side: programming the Buggy in Arduino language. As the hardware is imposed, the first step is to identify which Arduino pins correspond to the Buggy features. Here’s what I found so far:
As you can see in the above table, the headlights are controlled by Arduino pin 22, the main beam is controlled by pin 42 and breaks are turned on by pin 28. The left turn signal is pin 62, while the right turn signal is pin 24.
Driving the motors
Unlike the ubiquitous L298N which controls the power in many robots and rovers, the MikroElektronika Buggy uses a pair of DRV8833 H-bridges from Texas Instruments. An interesting choice, which allows for some extra control as compared against the L298N.
Some terms in this table attract attention: fast decay and slow decay. What the heck is this? Read the following paper for a full explanation of what slow decay and fast decay are, and how the current flows in these modes. Believe me, it’s worth reading it.
Another thing to consider is how the motors are connected inside the Buggy. There are two DRV8833 ICs, each controlling the motors on one side. IN1 and IN3 are connected in parallel, OUT1 drives the front motor and OUT2 drives the rear motor.
If you look at the original PIC32 example code from Libstock, you’ll find out that only fast decay is used. I will keep the same approach for the moment. If you wish, it takes only a few keystrokes to change to slow decay.
Based on the pin correspondence in the first table, the pins to control the left motors are 25 (PWMA) and 29 (PWMB), while the right motors are controlled by pins 37 (PWMC) and 53 (PWMD).
With all the above considerations, here’s a demo code to control the motors and the LEDs in the Buggy:
You may notice that when I run the Buggy at a slower speed I first drive the Buggy at full PWM for a few milliseconds to overcome the inertia. Think of a regular car – you start in the first gear, which gives you the maximum torque. Here we have no gears to change, only a fixed redactor. So, to get the maximum torque you need to apply full power to the motor.
Second, by changing the PWM ratio between left and right motors the Buggy can make a wide turn. Sharp turns can be achieved either by stopping one side or by turning it into reverse (which is the way I did in this code example). In a way, the Buggy turns more like a tank rather than a car.
And some final thoughts
Controlling the Buggy in MPIDE allows benefiting from the best of two worlds – the excellent hardware of the MikroElektronika Buggy and the huge amount of already written Arduino code. There are also libraries for almost any sensor you can imagine, but sometimes they don’t work on PIC32 – this happens because some coders use “pure” Atmel 8-bit C code inside the libraries to achieve some code speedup. There is nothing wrong with that, but it may prove a nuisance as the line of microcontrollers that can be programmed in Arduino is growing. In many cases, all you have to do is search for a library from a different coder.
Furthermore, MPIDE is free. So, if you are a hobbyist or a student looking for an idea for the final year project, this might be a good starting point – and a good way to save some money if you don’t already own some PIC32 development tools.
The code examples that I gave here are just that – some demos. Please feel free to enhance them and add your own contribution. I’ll be glad if you share your results.
And don’t forget: Have fun!