Thursday, July 18, 2013

Getting it all together -- Raspberry Pi talking to the Arduino UNO talking to the L298N dual H-bridge makin’ wheels turn





All setup for some code and connection testing

My project manager (Kylie) is back home now so the project engineer (me) is trying to get a step or two ahead of her…  She’s expecting progress by the time we get together again!

Power:  I’m using a rechargeable LiPo USB battery to power the +5V for the RPi and Arduino and a separate set of 8 rechargeable NiMH AA’s (9.6V) for the motor power to the L298N.

Motors and motor drivers:  The chassis we’re using has 4 DC motors (3-12V) and I’m using a SainSmart L298N dual H-bridge module to drive each pair.  At the moment I only have one H-bridge, so for testing I’m driving the two rear motors.  My plan is to connect the second H-bridge to the front motors in a way that the code will drive the left and right wheels as pairs for maneuvering (if anyone knows why that’s a good or bad idea please let me know…). 

Arduino to L298N:  The connections from the Arduino to the L298N are pretty straightforward.  See the pics at the side of this page for reference.  If you want to vary the motor speed you have to remove the jumpers the ENA and ENB pins in order to provide a PWM signal from the Arduino. Then you have to provide a digital output pin for each of the four inputs on the L298N (IN1 & IN2 control motor A and IN3 & IN4 control motor B).  Finally, don’t forget to connect GND – GND.  Of course the NiMH battery pack is connected to the motor power block with + to VCC and – to GND.  You can leave the 5V empty since the board has an onboard regulator to provide the logic circuits the 5V from the motor power inputs. 

So, my pin connections are:

Arduino (digital)           L298N            Comment

pin 2                               IN1                  motor A direction
pin 3                               ENA                motor A speed; needs to be a PWM output pin
pin 4                               IN2                  motor A “brake”

pin 5                               IN3                  motor B direction
pin 6                               ENB                motor B speed; needs to be a PWM output pin
pin 7                               IN4                  motor B “brake”

In the Arduino sketch, you control the motor direction by setting IN1 (or IN3 for motor B) to HIGH or LOW and then “releasing the brake” by setting IN2 (or IN4 for motor B) to the opposite.
i.e.:
IN1 = HIGH  using digitalWrite
IN2 = LOW   using digitalWrite
ENA = 127   using analogWrite to set PWM
Will spin the motor in one direction at 50% (PWM values go from 0 – 255).  Reversing IN1 and IN2 will reverse direction. 

Hope that was clear enough…  I’ll post the code as soon as I get it initially tested.

BTW, here is a good place to make some acknowledgements:
1.    Username cbrittian1 provided a super Instructable (see link) that I followed pretty closely to get up and running and I’ve shamelessly “stolen” his code as the core for my project.  That’s the beauty of open source…  hopefully I’ll contribute some modifications and improvements along the way.

2.    At work we have an excellent robotics team, and Scott in particular has been a key “consultant”, turning me on to the Raspberry Pi in the first place, explaining the electronics details, and  pointing out a few things that should have been more obvious to me but weren’t.

3.    And of course there are many, many other great discussions and contributors out there that I’ve leveraged for this project.  For example, this YouTube video  is excellent for setting up your USB WiFi dongle.  It gives a link to a script as well.

RPi talking to the Arduino:  I plan to use the I2C for communications to / from the RPi and Arduino.  That’s the way cbrittian1 did it.  I’ve written and tested some very basic USB serial code but I didn’t want to rewrite his code, so I2C it is, at least for now.  Plus, by powering the Arduino from the 5V GPIO from the RPi, the vehicle won’t have to carry around the USB cable and I can still use it for code development and uploading.  I’ve ordered a couple 4-channel I2C-safe Bi-directional Logic Level Converters but haven’t received them yet.  Even though there are a few articles out there saying if you have the RPi set up as MASTER that it’s OK to direct connect the SDA / SCL pins because they have pull up resistors, I don’t want to take that chance yet so, now I wait…


Sunday, July 14, 2013

SainSmart 4WD Chassis, Raspberry Pi & Arduino UNO


Great progress!

 
Kylie was visiting for a few days and we made some great progress.

She assembled the entire chassis, it’s a SainSmart 4 motor chassis with two acrylic platforms for mounting electronics.  More on that in the next post…  Then she did the entire setup of the Raspberry Pi.  I had preloaded the SD card and put it in the clear case to make it easier to handle, but she unpacked the Arduino and the motor driver (L298N) so she learned how important anti-static packaging is and how to handle the boards carefully from the sides.  She did great!  Then she began the Pi setup by learning that it needs inputs (USB keyboard and mouse);  then it needs some way to show us outputs (HDMI to a monitor); and finally it needs power.  Then Kylie threw the switch and the Raspberry Pi booted up perfectly and came up with the great Raspberry Pi logo and all the Raspbian start up code.  She was very exited!  Then we connected the Ethernet cable to get internet and she typed “startx” and got the graphical interface.  She loved it and began playing with Scratch, a super programming language from MIT designed for kids ages 8 & up.  In no time she was “programming” Scratch projects.  She has a special project in mind for Mom, so Kylie will share my account on Scratch 2 for Windows so we can work on it together when she’s home.

Friday, July 5, 2013

SD cards and batteries -- Raspberry Pi

Just a quick post to document some problems and successes.  


SD cards:  Even though I bought a preloaded 4GB SD card, after I got a little comfortable with the Pi I wanted to try to load one myself.  I bought a good quality 16GB card and downloaded Berryboot, unzipped, and copied to the SD card. I popped it into the Pi and powered up. My PC monitor has a slot for an external HDMI so I could watch the boot process, but I didn't have a USB keyboard or mouse so I couldn't do anything with it.  So the next day I borrowed the keyboard and mouse and off we went. When i originally booted with the preloaded card, I didn't need the keyboard and mouse since it comes with SSH enabled and after finding the IP address I could just open a PuTTY terminal session from my PC.  Berryboot was pretty easy to set up but I encountered a few error messages in the process of using raspi-config.  I have a widescreen HD monitor and I couldn't get the configuration to yield outputs that weren't off screen on the left side. Enabling and disabling overscan didn't have any effect. At this point I don't see a need for multiple boot OS options, I'll be working Debian (Raspian) and I want the boot to allow me to I immediately open a SSH PuTTY session so I abandoned the Berryboot route.  That meant I needed to reformat and reload the SD card.  This time I downloaded and used the SD card formatter and the New Out Of The Box (NOOB) software load from the Raspberry Pi site. Following their instructions everything worked like a charm!  Unless you are going to be heavy into development and need different operating systems on a single SD card, I highly recommend sticking to the NOOB load and instructions.  After inserting and booting the new card, I completed the process by using $ sudo apt-get install tightvncserver to allow a remote graphical session. Finally, I followed some instructions from "How to Make a Arduino+Raspberry Pi Robot Platform by cbrittain1 on June 18, 2013" on "Instructables, Let's Make Robots" (a great source!) to setup several useful software elements.

Battery power:  I happen to have a rechargeable (LiPo) battery with 4 USB ports for powering multiple devices.  I use it all the time when traveling to charge my iPhone and iPad.  You can use it plugged in so that it recharges while charging devices and it has a power switch. So this seemed perfect to use with the Pi. Each USB output can deliver up to 2.1A at 5V up to to a total of 2.5A and it's ~8000mAH.  I tried it out without having it plugged in and it provided perfect power for the Pi for several hours.  The fact that it has multiple USB ports means I can use it to power the Arduino simultaneously when I get to that point. The battery I have is from iSound and while it's not cheap (~$60) and it's not tiny, I think it'll be fine for the robot power until I find a smaller solution.  

Tuesday, July 2, 2013

Unpack & Initial Setup -- Raspberry Pi



 
 
Hard to believe this is going to be a robot…

 

Most of our parts arrived yesterday, some assembly required…  Here’s the list so far:

·         Raspberry Pi 512 MB + Clear Case + Micro USB Power Supply + Preloaded SD Card + HDMI Cable

·         Arduino UNO R3

·         L298N Dual H Bridge Stepper Motor Driver Controller Board Module

·         2x Ultrasonic Module HC-SR04 Distance Sensor For Arduino

·         4 Wheel Drive (4WD) Arduino Robot Platform

I put everything away until my project manager (Kylie) comes to visit next week, except the RPi.  I thought I’d better get a step ahead of her on that one.  So, I put it in the case (highly recommended, it holds the RPi steady with all the cables attached and protects against static discharge), connected an Ethernet cable from the router, connected the HDMI cable to a monitor, inserted the preloaded SD card (highly recommended for first time users), and holding my breath, plugged it in.  I was reassured quickly with flashing lights and the monitor came alive with the boot process.  All seemed well!  Except since I didn’t have a USB keyboard and mouse I couldn’t do anything with it. 

My intention all along was to use the PuTTY SSH to establish a terminal session, and that actually turned out to be very easy.  After downloading and starting PuTTY, the hardest part is determining the IP address of the RPi.  That’s relatively easy by accessing the router configuration from the PC.  After setting up the terminal session I was in business.  The next task was to install VNC on the RPi and the PC to be able to get the graphical interface.  Following some very good instructions online at “Let’s Make Robots” this turned out to be pretty easy as well.  I installed the tightvncserver on the RPi  and RealVNC client on the PC.  Then ran tightvncserver with the appropriate geometry settings and opened the RealVNC client with the IP address and port (don’t forget the “:1”) and viola, the Raspian graphical interface came alive.  Finally, I set my router to reserve the RPi’s IP address so I will always know it.  This is instead of setting the configuration on the RPi to a static IP, which I haven’t figured out yet.  I’m not sure the reserved IP address will work if I had more than one RPi on the network at the same time, but that’s another day. 

All in all a very successful start for the “engineering” team on the project!  It will get harder since Linux / Raspian and Python are all new to me.