Using MPLAB.X on Ubuntu 11.04 64bit with Dwengo board

I have been using Pinguino for quite some time to program my Dwengo board from within Linux, but as the source code grows larger, the Pinguino “sketches” aren’t always the most convenient code structure. In addition, and more important, I notices some weird unstable behavior where initializing a string variable with string A worked fine but initializing it with another string resulted in the code not being able to compile. As Microchip has released a cross-platform IDE called MPLAB X, I tried setting it up on my 64bit Ubuntu (v11.04 aka the Natty Narwhal) today. This article is a step-by-step tutorial on how to compile your code with MPLABX, load it on the Dwengo board and run or debug it using the Dwengo programmer.

  1. Go to the MPLABX download page, select “Linux x86 (32 bit)” as the platform (there is no 64 bit version) and check the boxes “MPLAB IDE X” and “MPLAB C18 Lite Compiler for PIC18 MCUs”. Then hit “Download now”. Do it fast, because a reeeeally annoying video (which you can’t stop) automatically starts playing upon loading the page!
  2. Two installer files will start downloading, make them executable:
    chmod +x ./mplabc18-v3.40-linux-full-installer.run
    chmod +x ./mplabx-ide-beta7.02-linux-32-bit-installer.bin
  3. You can try to run the graphical installers (just double-click the files), but on my 64bits Ubuntu this gave the elaborate “Segmentation fault” error message. The problem is documented here, and solved by running the installers in text mode. So first install the C18 compiler:sudo ./mplabx-ide-beta7.02-linux-32-bit-installer.bin --mode text

    and then install the IDE

    sudo ./mplabc18-v3.40-linux-full-installer.run

    edit: @markgross got the graphical installer working, as explained here

  4. Just one more hurdle to go! As the MPLABX is a 32bits version and uses Java, it expects to run on a 32 bit java virtual machine. However, the default Java you’ll install on Ubuntu via the package manager is a 64bits version. Follow the instructions on this site to run the IDE using a 32 bits Java version. Basically you install a 32 bits Java in some folder and create a shell script which points the JAVA_HOME and PATH environment variables to this Java.
At this point, you installed the environment succesfully and you should be able to start it without getting any errors. I start the script created in step 4 with sudo, because otherwise I do get some errors (and I’m not that into sorting out all kinds of file permissions) of files being read-only.

Now we will install the Dwengo library. Download it as a zip file from here, and extract it. You’ll get two folders, one with a .lib file in it and another one with the header files (.h extension). In a terminal, cd to the extracted folder and copy the .lib file to the lib folder of mplabc18 and the .h files to the /h/ folder (the exact destination folder can differ on your system depending on where you installed C18):
sudo cp ./lib/dwengo.lib /opt/microchip/mplabc18/v3.36/lib
sudo cp ./h/*.h /opt/microchip/mplabc18/v3.36/h/

Ok, we are set to go! Fire up the MPLABX IDE and let’s create a project… In the file menu, click “new project”. A dialog pops up. Select “Standalone Project” and click “Next”.

The Dwengo board ships with a PIC18F4550, so select this as the device:

The Dwengo programmer is compatible with PICkit2, a MicroChip programmer/debugger device. So select this as the tool and click next. The nice thing about using this device with the MPLAB software is that we will be able to actually debug our code! That is breakpoints, step-by-step and other goodness instead of tediously writing variables to the LCD.

Select C18 as the compiler toolchain:

Finally, name your project:

A project structure is created, shown on the left. We will start out by adding the Dwengo library by right clicking “Library Files” and selecting “Add existing file”. Select the .lib file we downloaded earlier.

Next we add the linker file, needed for successfully compiling your code. Right click on “Linker files” and select the file 18f4550_g.lkr which you’ll find in the folder /opt/microchip/mplabc18/v3.36/bin/LKR (or something similar, depending on where you installed C18).

And now, finally, we can add some actual code🙂

I used the Dwengo “blinking Leds” example, which you can find here. Once the code is added, right click your project and click “clean”. This will remove any previously compiled code and asserts that you are not working with any old version of your code.

Now right click your project and click “build”. I got a lot of errors on the console window, but clicking “build” once more successfully compiles and links all code. Perhaps this is some error with the IDE (remember this is still a beta), so remember to hit “build” twice every time… You should get output similar to this if the building succeeds:

make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
make[1]: Entering directory `/root/MPLABXProjects/the_stalker.X'
make  -f nbproject/Makefile-default.mk dist/default/production/the_stalker.X.production.cof
make[2]: Entering directory `/root/MPLABXProjects/the_stalker.X'
make[2]: `dist/default/production/the_stalker.X.production.cof' is up to date.
make[2]: Leaving directory `/root/MPLABXProjects/the_stalker.X'
make[1]: Leaving directory `/root/MPLABXProjects/the_stalker.X'

BUILD SUCCESSFUL (total time: 162ms)
Loading /root/MPLABXProjects/the_stalker.X/dist/default/production/the_stalker.X.production.cof...
Loading completed

And now the moment you have all been waiting for... Make sure you connected the programmer with USB to your computer and that the programmer is plugged into the Dwengo board. Hit "Run project" in the "run" menu. You should get the output

Connecting to programmer...
Programming target...
Programming completed
Running target...

You can also double click a line in the code and run “Debug project” from the debug menu. Now you can step through the code to inspect variables and check the flow of your code.. nice!

I hope this guide helps anyone also trying out the new MPLAB X beta on Ubuntu. The installation is a bit difficult, but it looks like a really cool product. Especially the debugging is something that will save me countless frustrating hours🙂

August 28, 2011 at 8:44 pm 4 comments

Introducing the_stalker: controlling a camera using Dwengo

I finally found time to play with some electronics again this weekend, so I decided to dismantle my old camera and try to control it from my Dwengo board. A simple plan, so let’s go ahead! The camera is a cheap Aiptek DV3300 camera / camcorder which was my trusted companion for many years and with which some of Overimpe’s best underground heist, crime and weird psychedelic space-time-travelling movies were shot.

First I had to disassemble the camera, which went fairly easy (some screws and some careful pulling here and there). Once inside the camera, I saw that the plastic buttons on the outside just pushed some standard pushbuttons on the inside. After measuring the voltages when pressed / released, the various buttons seem to just pull a line to ground upon being pushed.

Because the buttons are placed fairly close to each other, and my soldering skills are a bit rusty, I decided to start with just controlling two buttons: the power switch and the shutter button. This would allow the microcontroller to start the camera, take a picture (or record a video), and power off the camera. So I soldered two wires to the buttons, put a big blob of hot glue on it (see picture below) and guided the wires to the bottom of the camera, where I could make them come out of the camera through the removed screw of the tripod.

Below you see the re-assembled camera, with some wires coming out of it at the bottom.

And below is the entire test setup. The Dwengo microcontroller board on the right listens on a serial interface (connected to a laptop) for commands telling it to power on the camera and to start recording. Once these commands are received, a pin on the PIC microcontroller is pulled HIGH. This pin will make a transistor go into saturation and connect the camera line (power or record) to ground for a few seconds. This simulates a button on the camera being pushed.

For those interested, the breadboard layout is shown below (click on it to open a larger version). I only included one line in the schema, because the second line is basically the same circuit duplicated. More information about how the circuit works can be found on this excellent site.

And a video as final proof that it works🙂

The code can be found on github. It is really, really ugly code though, because I had a LOT of problems getting it to run ok (I think something is wrong with my Pinguino bootloader). The code will be cleaned up in future commits though.

The next steps for this project will be to move from a breadboard to a soldered circuit and maybe use Bluetooth instead of a serial cable. I think in the end I will try to embed the circuit in a bird-house in my garden to spy on those shady birds, and trigger the video recording with a motion detector (when a bird enters the house). I’m not sure about that yet though, any ideas are welcome🙂

PS: Thanks to the Dwengo people who gave me a free Dwengo starterkit plus as a gift in appreciation of my blog! So if you do a cool project with the Dwengo board, be sure to write about it😉

July 11, 2011 at 10:16 pm 1 comment

Creating a custom Fritzing part for the Dwengo board

A problem I always have with my robots is they end up as a messy spaghetti monster with lots of wires leading to hidden places on the robot. When (purely hypothetically) the robot bumps into a wall some wires get loose and I have to figure out what was connected to what again.

I should keep some documentation on my robot. I wanted to do this with Fritzing, a cool open-source program for drawing breadboard-prototypes and more. Lots of parts are already available in Fritzing, like an Arduino, a servo, chips, and so on. As the Dwengo board was not yet available as a Fritzing part, I created it myself.

A Fritzing part is composed of SVG vector graphics: one for the breadboard layout, one for the schematic layout, one for the PCB layout, and one for the icon. These graphics can be created in InkScape. I never worked with InkScape before, so I had a hard time drawing the breadboard layout. The easiest way to create the layout was to take a photograph of the board, then import that in Inkscape, and draw over it. I reused the svg images from the existing Fritzing core parts to create the final layout.

Because Fritzing has to know which part of the image is a connector where wires can start and end, one has to create connectors. This is done by creating rectangles with the names connectorXterminal and connectorXpin (where X is the connector number). In the schematic and PCB layout, the same connector have to exist with the same name. This way, Fritzing can keep the three layouts in sync, so if you create a prototype in the breadboard view, you immediately get a schematic view and a PCB view.

For the SVG files of the schematic view and the PCB view, I started from a header row (for the schematic view) and from a regular chip (for the PCB). Once these were done, I created the part in Fritzing:

In the parts editor, you can label all the connector pins and give them a description. You can also add and remove connectors if necessary. As you can see from the image, I created connector pins for the Dwengo connector on the left, and for the driver chip output on the right. The pin labels mention the PIC pin number and the corresponding Pinguino pin number (see https://kr3l.files.wordpress.com/2009/12/pinguino.gif).

Download the Fritzing part.

Now that the part is created, the fun can start🙂. It only takes about 5 minutes to redraw my robot schematic in Fritzing. One problem I had was that the Bluesmirf component doesn’t exist in Fritzing, but I used a “mystery component” with 5 pins instead. The resulting prototype is shown below:

As you can see, this is a nice and  easy way of creating a breadboard prototype of your project. You can download this diagram from its Fritzing projects page. Fritzing features a lot more cool features, like automatically create a parts list, create an etchable PCB layout or Gerber files as a Pdf, …, which help you in sharing your project with others and in going from a prototype on a breadboard to a real product on a PCB!

January 8, 2011 at 10:10 pm 4 comments

Bluetooth communication between BlueSMIRF and Ubuntu

A quick post on how to set up the Bluetooth communication between Ubuntu and a BlueSMIRF Modem.

My laptop was broken recently, and after getting a free new motherboard from the kind Sony people I reinstalled Ubuntu and tried to get all my software running again. Today I tried setting up the Bluetooth communication with my toy car’s BlueSMIRF (see previous posts) and  my laptop, and decided on documenting it. So here’s how to do it:

  1. (optional) It’s easiest if you can see what is happening on the BlueSMIRF side. Therefore, you’ll need a serial (TTL) to USB convertor like this one. Alternatively, if you have a Dwengo board (or similar), you can use that one by simly removing the PIC18F chip for a while:

    Now you can monitor the commands and data passing through the BlueSMIRF by opening “serial port terminal” (install it through the Ubuntu Software Center) on /dev/usbtty0 at the speed of your BlueSmirf (default 115200 baud)

    Details on the commands available are available in the Roving Networks Command datasheet.

  2. I didn’t get the default Ubuntu Bluetooth tool (the B in the taskbar) to play nice with the BlueSMIRF, so I installed “Bluetooth Manager” (BlueMan) through the Software Center.
  3. Power the BlueSMIRF, it should start blinking rapidly for about a minute. This means it is waiting for a connection. Once the rapid blinking stops, you can’t connect anymore!
  4. Click on the B in your taskbar of BlueMan, a window should open showing the Bluetooth devices in your environment. One of them should be FireFly (the BlueSMIRF). Right click on it an select “connect to SPP”:

    (the name in the screenshot is robot-719A instead of Firefly because I changed it earlier)
  5. Because this is the first time you connect to the BlueSMIRF, it needs to be paired. Therefore, Blueman will ask you the pass phrase. Enter “1234” (without quotes), this is the default passphrase of the BlueSMIRF.
  6. The bottom of the Blueman screen should now say “Serial port connected to /dev/rfcomm0”. Now you can open a serial connection to /dev/rfcomm0 using serial port manager (or from within a script) and start communicating🙂
    The LED on the BlueSMIRF should turn green.

That’s it! Now the pairing is done, and you won’t need to enter the passphrase the next time you connect to the BlueSMIRF.

    December 19, 2010 at 11:15 am Leave a comment

    Using Android as robot remote control

    The flea market car / robot saga continues… Controlling the robot from my laptop via Bluetooth was cool
    , but running after the robot with the laptop in my hands kind of diminished the James Bond – feeling of it. Therefore, I decided that my first application I would create for my brand new Android smartphone would be a remote control for the robot!

    Luckily, the online developer information (available at http://developer.android.com), provides a wealth of information for getting started. What was more, I actually found a sample application there, called BluetoothChat, that almost did exactly what I wanted to do. The BluetoothChat application makes a connection to a Bluetooth device (this will be the robot) and it allows to send text to it once connected (the text will be the robot commands).

    Google provides a complete development environment for developing Android applications, which can be downloaded at http://developer.android.com/sdk/index.html. Just follow the instructions over there to install the platform and create a “hello world” application. Connecting your phone over USB to the development computer allows you to debug / run the application on the phone. On my Ubuntu laptop, I have to restart the adb server as root after connecting the phone via USB:

    (while in folder /android-sdk-linux_86/tools)
    ./adb kill-server
    sudo ./adb start-server
    ./adb devices

    Now let’s skip to the application. I wanted to change a few things to the BluetoothChat application:

    • actually get it connected to a Bluetooth device, because this wasn’t working right away..😦
    • after connecting to the robot over Bluetooth, I wanted to automatically start my PING – PONG handshake (Android says “PING” and the robot answers with “PONG”, which indicates the connection is established successfully and the robot should start listening for commands)
    • Instead of typing the commands in the text field I wanted some big buttons (forward, backward, left, right, …)

    Connecting the BluetoothChat example to a Bluetooth device

    I first tried connecting the BluetoothChat Android application to Bluetooth on my laptop, but it didn’t work. Whatever I tried, I always got a “service discovery failed” exception on the following line of code in BluetoothChatService.java:

    tmp = device.createRfcommSocketToServiceRecord(MY_UUID);

    After a long time trying to solve this, I found this site, which suggested to change the line of code to this code:

    Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
    tmp = (BluetoothSocket) m.invoke(device, 1);

    And this worked! Also, note that you have to connect at 57600 baud to the Android Bluetooth.

    Do a handshake once connected

    Once connected to the BlueSMIRF on the robot, the following should happen:

    • Android sends “PING\n” to robot
    • Robot receives this and sends “PONG\r\n” to Android
    • Android receives this, and knows the connection is succesfully established.
    • Now Android can send commands like “D,70,1” to set the servo to 70° and the drive motor to forward motion.

    I changed the run() method of the ConnectedThread to do the handshake once connected: see code.

    Big buttons that send commands to the robot

    And now for the fun part.. I wanted to change the layout to have nine big buttons which would send the robot in a certain direction:

    I used the example at this site to create the big button layout. Once a button is pressed, it will send a command over Bluetooth to the robot which will make it ride in that direction:

            mBRButton = (Button) findViewById(R.id.button_br);
    
            mBRButton.setOnClickListener(new OnClickListener() {
    
                public void onClick(View v) {
    
                    // Send a message using content of the edit text widget
    
                    String message = "D," + right + "," + backward + "\r\n";
    
                    sendMessage(message);
    
                }
    
            });

    And now a little movie to prove that it works!

    The source code can be downloaded here.

    edit: latest version of code is now available via https://github.com/kr3l/rommelrubot

     

    September 4, 2010 at 5:18 pm 14 comments

    Time for a new steering system

    My last post was about putting Bluetooth in a flea market toy car in order to control it remotely from my laptop. One problem I had with the car was the amount of current it drew. Unexpectedly, it turned out to be the steering that drew most of the current. I thought the steering was done by a servo motor, but apparently they put a regular DC motor in it to turn the weels. A piece of plastic stops the turning and basically the motor stalls everytime you steer, and so draws the maximum amount of power..

    To reduce the power-demands of the car I cut out the steering DC motor (and all the plastic around it) and glued in a servo.

    Now to make the servo turn the weels, I used some good old Lego bricks. The flat one with the dents is glued to the thing that used to make the weels turn. Than I used a gear wheel glued to (God, I love hot-glue!) the servo, which makes the flat brick move. Because the servo sometimes turns too much (it’s a cheap one so not always that accurate), I cut most dents from the gear wheel. This assures that no damage is done if the gear wheel turns too much..

    August 15, 2010 at 9:34 pm Leave a comment

    Putting Bluetooth into a flea market toy car

    On the flea market here in Ghent a few weeks ago, I bought myself a toy car with a wired remote control. Feeling destructive, I immediately removed the hood of the car, opened up the remote control and measured how the car could be controlled. It turns out there are four wires from the remote to the car. Two of them control the forward / backward motion by setting 5V or -5V across them. The two other wires control the left / right motion of the front wheels, again by setting 5V across them.

    Next I soldered header pins to the wires of the car so I can control them through the L293D driver chip of my Dwengo board. Next I added the Bluesmirf module (as explained in a previous post) and taped everything together using Duct tape.

    After some programming and some tweaking (putting the battery pack above the steering wheels so they have more grip) I got my car driving across the appartment🙂

    edit: latest version of code is now available via https://github.com/kr3l/rommelrubot

    May 29, 2010 at 8:39 pm Leave a comment

    Older Posts


    Feeds

    Articles to be written…

    Twitter – kr3l

    my del.icio.us

    RSS Google Reader Shared Stuff

    • An error has occurred; the feed is probably down. Try again later.

    RSS Listening to..

    • An error has occurred; the feed is probably down. Try again later.

    Follow

    Get every new post delivered to your Inbox.