Skip to content

Basic Commands

Carlos Viescas edited this page Aug 11, 2019 · 11 revisions

3.1. Installing Software

After installing all the requirements in the previous page, get the robot's main software. To do so, use the following commands on the raspberry pi (assuming it is Raspbian Stretch):

# Download source
$ mkdir ~/Workspace
$ cd ~/Workspace/
$ git clone https://github.com/CVH95/Canyonero

# Install C++ Source
$ cd Canyonero/code/
$ mkdir build bin libs
$ cd build/
$ cmake ../
$ make

Installation of ROS packages is addressed on the next page.

3.2. Handling RPi GPIO Pins.

For the full schematics, see the Robot Design page of the wiki.

3.2.1. Motors.

DC Motor 1:

  • Pin 1: GPIO 17.
  • Pin 2: GPIO 22.
  • Enable: GPIO 16.

DC Motor 2:

  • Pin 1: GPIO 23.
  • Pin 2: GPIO 24.
  • Enable: GPIO 20.

Servo Motor 1 (PAN):

  • Vcc (red wire): +5V (Pin #2 on the RPi).
  • Signal (orange wire): GPIO 18.
  • GND (brown wire): GND (Pin #6 on the RPI).

Servo Motor 2 (TILT):

  • Vcc (red wire): +5V (Pin #2 on the RPi).
  • Signal (orange wire): GPIO 25.
  • GND (brown wire): GND (Pin #6 on the RPI).

3.2.2. Sensors & others.

HC-SR04 Ultrasonic sensor:

  • Vcc: +5V (Pin #4 on the RPi).
  • TRIGGER: GPIO 12.
  • ECHO: GPIO 6.
  • GND: GND (Pin #39 on the RPI).

White LEDs

  • Right LED: GPIO 21.
  • Left LED: GPIO 13.
  • GND: GND (Pin #34 on the RPI).

3.2.3. Software handling.

The core code for controlling Canyonero is implemented in C++. There are also some basic scripts in python to quickly test different functionalities. To manipulate the GPIO pins with C++ we use WirinPi Library. Basically, GPIOs on WiringPi can be easily set up with the following code:

#include <wiringPi.h>

#define GPIOnumber 18
#define MotorGPIOnumber 22

using namespace std;

int main()
{
  int dutyCycleValue = 50; // in ms

  // To set up WiringPi
  if(wiringPiSetupGpio() == -1)
  {
	return -1;
  }

  // To define the function of the given pin (OUTPUT (motor, leds..) / INPUT (sensor))
  pinMode(GPIOnumber, OUTPUT);
  pinMode(MotorGPIOnumber, OUTPUT);

  // If needed, to create a software based PWM (like to drive motors)
  softPwmCreate(MotorGPIOnumber, 0 , 200); // from 0 to 200 ms cycle
  
  // Run for some time
  for(int i=0; i<1000; i++)
  { 
     // To set the value of the pin (LOW=0 / HIGH=1)
     digitalWrite(GPIOnumber, 0);

     // To change the duty cycle of the PWM
     softPwmWrite(MotorGPIOnumber, dutyCycleValue);
  }

  // Clean up GPIOs by setting them back to INPUT
  pinMode(GPIOnumber, INPUT);
  pinMode(MotorGPIOnumber, INPUT);

  return 0;
}

Check out the class gpioControl in include/gpioControl.h and src/gpioControl.cpp.

3.3. Video Stream.

Video streaming is supported through OpenCV's videoCapture. Input number for the USB camera is 0: (VideoCapture cap; cap.open(0)). The webApp transmits on real time. On ROS, the frame rate depends on the loop rate set for ros::spin(). At least, a minimum 20 Hz rate is required for a decent streaming. Less than that, the subscriber shows the stream with significant delay.

The ROS drivers do not perform any image processing, further than compressing the resolution to 600x480. Leave all processing to the control node. However, for heavy processing (like detection algorithms), reducing the loop rate might be a good thing.

3.4. Control codes (ROS interface)

The control codes correspond to int numbers that invoke actions. It is the way in which the ROS drivers read control inputs to run the robot. They allow easy coupling of any kind of external input, such as laptop keyboard strokes, a PS4 or Xbox controller, smartphone joystick app...

These are the control codes available for the moment:

  • 8: Drive forward.
  • 2: Drive Backward.
  • 6: Turn right.
  • 4: Turn left.
  • 7: Reduce speed.
  • 9: Increase speed.
  • 0: Stop robot.
  • 5: Stop robot, GPIO cleanup & exit program.
  • 11: Turn on/off lights.
  • 32: Lock/Unlock camera platform.
  • 21: Rotate camera up.
  • 22: Rotate camera down.
  • 23: Rotate camera right.
  • 24: Rotate camera down.

3.5. CPU and memory management on the Raspberry Pi.

With the full system in operation, the CPU consumption is between 36% and 39% (in ROS mode).