Day 21: MC-1 Part II: Software

Since programming and testing on the Raspberry Pi itself is not very productive, I have created a TCP/IP library to send commands to the ServoBlaster from a Windows .NET program. I named it PiEater. My Raspberry Pi has a WiFi USB token, so I can access it without the usage of cables.

I wrote the “Truck Driver” program for the MC-1:

TruckDriver

The speed slider changes the pulse width sent to the motors through the L293D. Since it is a little harder to find the center of the slider, I added a “Stop” button that does that. The slider allows going from full power to back to full power to front.

The camera position sliders act on the servos mounted under the camera. The “Center” button centers the camera in both  horizontal and vertical axes.

Day 17: Controlling a DC motor

I bought this car in a second-hand-stuff shop. Originally it was remote-controlled but when I bought it, it no longer had the remote control. I don’t know the motor specification. This car has five AA batteries (7.5V), so I think it is safe to use my 5V power source. I put it on a box to avoid it to run away.

???????????????????????????????

This time I used the L293D attached to GPIO 18 and 23 in a way I can make the motor run forwards or backwards.

???????????????????????????????

I used the Servo Blaster library as presented on Day 14. In order to run the motor forwards, I kept one signal at zero and changed the other one.


echo "2=0" > /dev/servoblaster

echo "5=500" > /dev/servoblaster

To run backwards, I inverted the signals:


echo "5=0" > /dev/servoblaster

echo "2=500" > /dev/servoblaster

It worked with values from 300 to 2000.

When the motor runs forwards, the white LED is on. The LED is connected after the L293D.

???????????????????????????????

When the motor runs backwards, the green LED is on. The LED is connected before the L293D.

???????????????????????????????

Day 14: Controlling a servo using ServoBlaster

I connected the servo control wire (the white one) in the GPIO-24 pin and connected an external 5V power source to the red and black wires:

???????????????????????????????

As usual, the external power source ground is connected to the Raspberry Pi ground.

Using the Servo Blaster library was pretty simple. The first step was setting up the library:


sudo ./servod --min=50 --max=250

By default, Servo Blaster assigns servo 6 to GPIO24. For my servos, the valid pulse width values go from 75 to 225 (750 µs to 2250 µs), centering in 150:


echo "6=75" > /dev/servoblaster
echo "6=150" > /dev/servoblaster
echo "6=225" > /dev/servoblaster

To stop the Servo Blaster deamon:

killall servod

Day 11: LEDs and PWM using ServoBlaster

For the hardware, I used the same circuit presented on Day 10.

In order to create the PWM, I used the ServoBlaster library by Richard Ghirst. It is part of the PiBits project (look for the “Download ZIP” button in the right side of the page).

Although it is possible to use the ServoBlaster deamon from the command line, I wrote a program to gradually turn the LEDs on and off:

???????????????????????????????

C source code:

/*
blink.c
Written by Wilson Medeiros (clockeater)
Revision 1.0 - 2013-12-07

This software requires that the ServoBlaster daemon (servod) to be running:
sudo ./servod --min=0 --max=2000

ServoBlaster is a third-party library written by Richard Hirst
*/

#include <stdio.h>

void blink();
void setLeds(int green, int red);
void wait();

int main (int argc, char* argv)
{
  blink();

  return 0;
}

void blink()
{
  int cycles;

  for(cycles=0; cycles<10 ; cycles++)
  {
    int value;

    for(value = 0; value <= 2000; value += 100)
    {
      setLeds(value, 2000 - value);

      wait();
    }
    for(value = 2000; value >= 0; value -= 100)
    {
      setLeds(value, 2000 - value);

      wait();
    }
  }
}

/*
Set the pulse width for the leds
Values must be between 0 and 2000 (1 = 10 us, 2000 = 20ms)
The green led is wired to GPIO 18 and the red one to GPIO 23
Each one is wired in series with a 330 ohm resistor
*/
void setLeds(int green, int red)
{
  FILE *f;

  f = fopen("/dev/servoblaster", "a");

  fprintf(f, "2=%d\n", green);
  fprintf(f, "5=%d\n", red);

  fclose(f);
}

/*
Wait for 1/10 second
*/
void wait()
{
  usleep(100000);
}