Controller development

The following code is an example of how you can work and create your own controller. We will be using AFMotor, Servo, and math libraries:


#include <AFMotor.h>
#include <Servo.h>
#include <math.h>

At first, we need to define all the pins we will be using. Notice that you can change them and define yours. In the following example, you must use Arduino Mega:


#define echoPin 24 // Echo Pin
#define trigPin 26 // Trigger Pin
#define LEDPin 13 // Onboard LED
#define objAvoidPin 22
#define leftEncPin 40
#define rightEncPin 43

AF_DCMotor leftMotor(4); // M4 - LEFT motor
AF_DCMotor rightMotor(2); // M2 - RIGHT motor
... and more

At this point, you need to initialize some variables. Define each pin, whether it is INPUT or OUTPUT:

void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Mobile robot up and ready...");
pinMode(objAvoidPin, INPUT);

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
delay(3000);
}

At this point, you need to create some functions to make your coding easier. For example, you can see some of them in the following code and understand that we need a function to get the distance of the front Ultrasonic sensor. Another one, to move forward, move backward, turn left and right.

The updateFrontDistance function reads the ultrasonic distance sensor and stores the result at a global variable. The following functions spin the motor forward or backward:


void moveForward(){
Serial.println(" Moving forward...");
leftMotor.run(FORWARD);
rightMotor.run(FORWARD);

leftMotor.setSpeed(180);
rightMotor.setSpeed(180);
}

void posStop(){
Serial.println(" Stoping...");
leftMotor.run(RELEASE);
rightMotor.run(RELEASE);
}

void moveLeft(){
Serial.print("moving left");
leftMotor.setSpeed(0);
rightMotor.setSpeed(150);
delay(1200);
rightMotor.setSpeed(0);
}

void turnRight(){
Serial.println(" Turning right...");
leftMotor.setSpeed(200);
rightMotor.setSpeed(0);
}

// Turns the servo 180 degrees and finds the position that the mobile can go forward
void findEmptyRoute() {

for(int i=10; i<160; i++) {
Serial.println("Increasing servo ");
servo.write(i);
if(distance <=25) {
updateFrontDistance();
Serial.print("Distance: ");
Serial.println(distance);
}

}
}

The FDSController function is used to detect an object and turn the mobile robot:

void FDSController() {
if ( (distance >= 35) && (distance <= 100)) { // Object detected
moveForward();
delay(1000);
}else {
turnRight();
delay(500);
}
}

Lastly, in the loop function, you can uncomment the part of code depending on your goal. Now, the controller is checking the distance of the front sensor and moves forward or turns right if an object is found.

More information can be found in the github repository.

Now, it is time to use our Raspberry Pi Zero W board to connect our Arduino with a device. You can do that using the Bluetooth module or even Wi-Fi. For example, one way is to set up a webpage with four buttons and connect to the webpage using your mobile phone or a computer. When clicking on a button, you will be able to send serial data from the Raspberry Pi to your Arduino.

Another way of communicating is using only the Bluetooth module and an Android application. With the Android application, you can control the movement of the mobile robot. Move to the front or back or turn to the right or left. There are various apps in the PlayStore and if you have some knowledge about Android development, you can always create your own.

When developing a controller, there are two things that matter. First of all, you have to decide what messages you will send from the controller to your Raspberry Pi board and then what messages you will send from the Raspberry Pi board to your Arduino controller. In your Raspberry Pi board, you can start a serial communication with your Arduino board using the Serial library. Type Python to open the shell:

python 

Then import the serial library by typing the following:

import Serial 

Then, create a serial communication with your Arduino by typing the following:

ser = serial.Serial(port, baudrate); 

Here the port is the port in which your Arduino is connected and the baud rate is up to you. Usually, the default baudrate is 9600. Remember that in Linux the port is something like /dev/tty***, whereas, in Windows it is something like COM*. By replacing these you can start the communication and now you should be able to talk from your Raspberry to your Arduino board.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.255.127