Lesson 1 Raspberry Pi Robot Car C Programming Project

Lesson 1 Raspberry Pi Robot Car C Programming Project

Lesson 1 Raspberry Pi Robot Car C Programming Project

Description

 

 

For this laboratory assignment, modify the lesson-1.c code to allow the user to control the robot’s motion from the keyboard. Associate the following keys to the corresponding command:

h or H – TURN LEFT

l or L – TURN RIGHT

i or I – MOVE FORWARD

m or M – MOVE BACKWARD

k or K – STOP

Keep the program running until the user enters a ‘q’ or ‘Q’ to stop the program.

Please submit your modified lesson-1.c code and a video of you running your robot.

 

/*  ___   ___  ___  _   _  ___   ___   ____ ___  ____

* / _ /___)/ _ | | | |/ _ / _ / ___) _ |   

*| |_| |___ | |_| | |_| | |_| | |_| ( (__| |_| | | | |

* ___/(___/ ___/ __  |___/ ___(_)____)___/|_|_|_|

*                  (____/

* Raspberry Pi Robot Car V2.0 Lesson 1: Basic Installation and Movement

* Tutorial URL https://osoyoo.com/?p=31599

*

*–From John– This lesson shows how to usse C++ to write digital data to PI’s GPIO port

* and send PWM (analog) signal to control basic movement (turning and speed.)

* Once compiled car will move forward for 1 sec…then backwards for 1 sed and turn left for 1 sec and

* then turn right for 1 sec.

*

* CopyRight www.osoyoo.com

*

*/

#include “pca9685/pca9685.h”

#include <wiringPi.h>

#include <stdio.h>

#include <stdlib.h>

 

#define PIN_BASE 300

#define MAX_PWM 4096

#define HERTZ 50

 

/*

* wiringPi C library use different GPIO pin number system from BCM pin numberwhich are often used by Python,

* you can lookup BCM/wPi/Physical pin relation by following Linux command : gpio readall

*/

#define ENA 0  //ENA connect to PCA9685 port 0

#define ENB 1  //ENB connect to PCA9685 port 1

#define IN1 4  //IN1 connect to wPi pin# 4 (Physical 16,BCM GPIO 23)

#define IN2 5  //IN2 connect to wPi pin# 5 (Physical 18,BCM GPIO 24)

#define IN3 2  //IN3 connect to wPi pin# 2 (Physical 13,BCM GPIO 27)

#define IN4 3  //IN4 connect to wPi pin# 3 (Physical 15,BCM GPIO 22)

#define SPEED 2000

 

// initialize  IN1,IN2,IN3,IN4

void setup(){

pinMode(IN1,OUTPUT);

pinMode(IN2,OUTPUT);

pinMode(IN3,OUTPUT);

pinMode(IN4,OUTPUT);

digitalWrite(IN1,LOW);

digitalWrite(IN2,LOW);

digitalWrite(IN3,LOW);

digitalWrite(IN4,LOW);

}

void go_advance(int fd,int speed){

digitalWrite(IN1,HIGH);

digitalWrite(IN2,LOW);

digitalWrite(IN3,HIGH);

digitalWrite(IN4,LOW);

pca9685PWMWrite(fd, ENA, 0, speed);

pca9685PWMWrite(fd, ENB, 0, speed);

}

void go_back(int fd,int speed){

digitalWrite(IN1,LOW);

digitalWrite(IN2,HIGH);

digitalWrite(IN3,LOW);

digitalWrite(IN4,HIGH);

pca9685PWMWrite(fd, ENA, 0, speed);

pca9685PWMWrite(fd, ENB, 0, speed);

}

void go_left(int fd,int speed){

digitalWrite(IN1,HIGH);

digitalWrite(IN2,LOW);

digitalWrite(IN3,LOW);

digitalWrite(IN4,HIGH);

pca9685PWMWrite(fd, ENA, 0, speed);

pca9685PWMWrite(fd, ENB, 0, speed);

}

void go_right(int fd,int speed){

digitalWrite(IN1,LOW);

digitalWrite(IN2,HIGH);

digitalWrite(IN3,HIGH);

digitalWrite(IN4,LOW);

pca9685PWMWrite(fd, ENA, 0, speed);

pca9685PWMWrite(fd, ENB, 0, speed);

}

void stop_car(int fd){

digitalWrite(IN1,LOW);

digitalWrite(IN2,LOW);

digitalWrite(IN3,LOW);

digitalWrite(IN4,LOW);

pca9685PWMWrite(fd, ENA, 0, 0);

pca9685PWMWrite(fd, ENB, 0, 0);

}

int main(void)

{

if(wiringPiSetup()==-1){

printf(“setup wiringPi failed!n”);

printf(“please check your setupn”);

return -1;

}

setup();

 

//

printf(“Lesson 1: Basic Movementn”);

 

// Setup with pinbase 300 and i2c location 0x40

int fd = pca9685Setup(PIN_BASE, 0x40, HERTZ);

if (fd < 0)

{

printf(“Error in setupn”);

return fd;

}

 

go_advance(fd,SPEED);

delay(1000);

go_back(fd,SPEED);

delay(1000);

go_left(fd,SPEED);

delay(1000);

go_right(fd,SPEED);

delay(1000);

go_right(fd,SPEED);

delay(1000);

stop_car(fd);

 

return 0;

}

 

Do you need a similar assignment written for you from scratch? We have qualified writers to help you. You can rest assured of an A+ quality paper that is plagiarism free. Order now for a FREE first Assignment! Use Discount Code "FREE" for a 100% Discount!

NB: We do not resell papers. Upon ordering, we write an original paper exclusively for you.

Order New Solution