Book Image

Hands-On Robotics Programming with C++

By : Dinesh Tavasalkar, Lentin Joseph
Book Image

Hands-On Robotics Programming with C++

By: Dinesh Tavasalkar, Lentin Joseph

Overview of this book

C++ is one of the most popular legacy programming languages for robotics, and a combination of C++ and robotics hardware is used in many leading industries. This book will bridge the gap between Raspberry Pi and C/C++ programming and enable you to develop applications for Raspberry Pi. You'll even be able to implement C programs in Raspberry Pi with the WiringPi library. The book will guide you through developing a fully functional car robot and writing programs to move it in different directions. You’ll then create an obstacle-avoiding robot using an ultrasonic sensor. In addition to this, you’ll find out how to control the robot wirelessly using your PC or Mac. This book will also help you work with object detection and tracking using OpenCV, and guide you through exploring face detection techniques. Finally, you will create an Android app and control the robot wirelessly with an Android smartphone. By the end of this book, you will have gained experience in developing a robot using Raspberry Pi and C/C++ programming.
Table of Contents (16 chapters)
Free Chapter
1
Section 1: Getting Started with wiringPi on a Raspberry Pi
4
Section 2: Raspberry Pi Robotics
8
Section 3: Face and Object Recognition Robot
12
Section 4: Smartphone-Controlled Robot

Moving the robot

Now that we have understood the H-bridge circuit, we will write a program called Forward.cpp to move our robot forward. After that, we will write a program to move the robot backward, left, and right, and then stop. You can download the Forward.cpp program from Chapter03 of the GitHub repository.

The program for moving the robot forward is as follows:

#include <stdio.h>
#include <wiringPi.h>

int main(void)
{
wiringPiSetup();
pinMode(0,OUTPUT);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);


for(int i=0; i<1;i++)
{
digitalWrite(0,HIGH); //PIN O & 2 will move the Left Motor
digitalWrite(2,LOW);
digitalWrite(3,HIGH); //PIN 3 & 4 will move the Right Motor
digitalWrite(4,LOW);
delay(3000);
}
return 0;
}

Let's see how this program works:

  1. First, we set the wiringPi pins (numbers 0, 1, 2, and 3) as output pins.
  2. Next, with the following two...