Library that will allow you to control movement, buzzer and sonar sensor

Dependencies:   HCSR04

Files at this revision

API Documentation at this revision

Comitter:
simon9987
Date:
Thu Mar 24 23:32:08 2022 +0000
Commit message:
New;

Changed in this revision

Buzzer.h Show annotated file Show diff for this revision Revisions of this file
CarHistory.h Show annotated file Show diff for this revision Revisions of this file
Common.h Show annotated file Show diff for this revision Revisions of this file
HCSR04.lib Show annotated file Show diff for this revision Revisions of this file
Movement.cpp Show annotated file Show diff for this revision Revisions of this file
Movement.h Show annotated file Show diff for this revision Revisions of this file
Sonar.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Buzzer.h	Thu Mar 24 23:32:08 2022 +0000
@@ -0,0 +1,46 @@
+#ifndef BUZZER_BUZZER_H
+#define BUZZER_BUZZER_H
+
+#include "mbed.h"
+
+DigitalOut Buzzer(p13);
+
+const float bREVERSE = 0.4, bTERMINATE = 1, bSTUCK = 0.4, bFLIPPED = 0.15;
+
+void buzzerReverse(){
+    Buzzer = 1;
+    wait(bREVERSE);
+    Buzzer= 0;
+    wait(bREVERSE);
+}
+
+void buzzerTerminate(){
+    Buzzer = 1;
+    wait(bTERMINATE);
+    Buzzer= 0;
+    wait(bTERMINATE);
+}
+
+void buzzerStuck(){
+    Buzzer = 1;
+    wait(bSTUCK);
+    Buzzer= 0;
+    wait(bSTUCK);
+}
+
+void buzzerFlipped(){
+    Buzzer = 1;
+    wait(bFLIPPED);
+    Buzzer= 0;
+    wait(bFLIPPED);
+}
+
+//used for reverse when getting to close to an object
+void buzz(const float &speed){
+    Buzzer = 1;
+    wait(speed);
+    Buzzer= 0;
+    wait(speed);
+}
+
+#endif //BUZZER_BUZZER_H
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CarHistory.h	Thu Mar 24 23:32:08 2022 +0000
@@ -0,0 +1,38 @@
+#ifndef TEMPLATE_PROGRAM_CARHISTORY_H
+#define TEMPLATE_PROGRAM_CARHISTORY_H
+
+#include <list>
+
+//store's a single car movement
+struct Value{
+    Value(){
+        action = 0;
+        value = 0.0;
+    }
+    Value(const int &action, const float &value){
+        this->action = action;
+        this->value = value;
+    }
+    int action;
+    float value;
+};
+
+std::list<Value> history;
+Value FORWARD, REVERSE, STOP, RIGHT, LEFT;
+bool toggleHistory = true;
+
+//store's values at the BEGINNING of list
+void add(const Value &value){
+    history.push_front(value);
+}
+
+//remove last added element from history
+void remove(){
+    history.erase(history.begin());
+}
+
+void clearHistory(){
+    history.clear();
+}
+
+#endif //TEMPLATE_PROGRAM_CARHISTORY_H
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Common.h	Thu Mar 24 23:32:08 2022 +0000
@@ -0,0 +1,5 @@
+#include "mbed.h"
+#include "Buzzer.h"
+#include "CarHistory.h"
+#include "Movement.h"
+#include "Sonar.h"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HCSR04.lib	Thu Mar 24 23:32:08 2022 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/prabhuvd/code/HCSR04/#71da0dbf4400
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Movement.cpp	Thu Mar 24 23:32:08 2022 +0000
@@ -0,0 +1,118 @@
+#include "Movement.h"
+
+PwmOut steering(p21);
+PwmOut velocity(p22);
+
+float forwardSpeed = 0.3, reverseSpeed = -0.3, turnAngleRight = 1, turnAngleLeft = -1, vo = 0.0;
+
+void Velocity(float v) {
+    v=v+1;
+    if (v>=0 && v<=2) {
+        if (vo>=1 && v<1) {
+            velocity.pulsewidth(0.0014);
+            wait(0.1);
+            velocity.pulsewidth(0.0015);    // move into reverseSpeed
+            wait(0.1);
+        }
+        velocity.pulsewidth(v/2000+0.001);
+        vo=v;
+    }
+}
+
+// Steering expects -1 (left) to +1 (right)
+void Steering(float s) {
+    s=s+1;
+    if (s>=0 && s<=2) {
+        steering.pulsewidth(s/2000+0.001);
+    }
+}
+
+//Always run this function!! Enables you to drive
+void setUpMovement(const float &forwardSpeedInput, const float &reverseSpeedInput, const float &turnAngleInput){
+    forwardSpeed = forwardSpeedInput;
+    reverseSpeed = reverseSpeedInput;
+
+    if(turnAngleInput > 0)
+        turnAngleRight = turnAngleInput;
+    else
+        turnAngleRight *= turnAngleInput;
+
+    turnAngleLeft = (float)(turnAngleRight*-1.0);
+
+    velocity.period(0.02);
+    steering.period(0.02);
+    Velocity(0); // initiate the drive motor (this must be done)
+    Steering(0); // centre steering
+    wait(0.5);
+}
+
+
+void moveForward() {
+    Velocity(forwardSpeed);
+    wait(0.1);
+}
+
+void moveReverse() {
+    Velocity(reverseSpeed);
+    wait(0.1);
+}
+
+void move(const float &speed) {
+    Velocity(reverseSpeed);
+    wait(0.1);
+}
+
+
+void steerLeft() {
+    Steering(turnAngleLeft);
+    wait(0.2);
+}
+
+void steerRight() {
+    Steering(turnAngleRight);
+    wait(0.2);
+}
+
+void steer(const float &angle) {
+    Steering(turnAngleRight);
+    wait(0.2);
+}
+
+
+void setReverseSpeed(const float &speed) {
+    reverseSpeed = speed;
+}
+
+void setForwardSpeed(const float &speed) {
+    forwardSpeed = speed;
+}
+
+void setTurnAngle(const float &angle) {
+    if(angle>=0)
+        turnAngleRight = angle;
+    else
+        turnAngleRight = angle*-1;
+    
+    turnAngleLeft = turnAngleRight*1;
+}
+
+void stop() {
+    Velocity(0.0);
+    wait(0.5);
+}
+
+void resetVelocity() {
+    Velocity(0.0);
+    wait(0.5);
+}
+
+void resetSteering() {
+    Steering(0);
+    wait(0.5);
+}
+
+void resetAll() {
+    Velocity(0); // initiate the drive motor (this must be done)
+    Steering(0); // centre steering
+    wait(0.5);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Movement.h	Thu Mar 24 23:32:08 2022 +0000
@@ -0,0 +1,37 @@
+//#ifndef HEADER_AND_CPP_FILES_MOVEMENT_H
+//#define HEADER_AND_CPP_FILES_MOVEMENT_H
+#pragma once
+#include "mbed.h"
+
+void Velocity(float v);
+void Steering(float s);
+void setUpMovement(const float &forwardSpeedInput, const float &reverseSpeedInput, const float &turnAngleInput);
+
+
+void moveForward();
+void moveReverse();
+void move(const float &speed);
+
+
+void steerLeft();
+void steerRight();
+void steer(const float &angle);
+
+/*
+void turnLeft();
+void turnRight();
+void turn(const float &angle);
+*/
+
+//set accelerate, de-accelerate and turn angle value
+void setForwardSpeed(const float &speed);
+void setReverseSpeed(const float &speed);
+void setTurnAngle(const float &angle);
+
+
+void stop();
+void resetSteering();
+void resetVelocity();
+void resetAll();
+
+//#endif //HEADER_AND_CPP_FILES_MOVEMENT_H
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Sonar.h	Thu Mar 24 23:32:08 2022 +0000
@@ -0,0 +1,61 @@
+#ifndef SONAR_SENSOR_SONAR_H
+#define SONAR_SENSOR_SONAR_H
+
+#include "mbed.h"
+#include "hcsr04.h"
+//#include "Print.h"
+#include <string>
+
+Serial pc(USBTX, USBRX); // tx, rx
+
+
+//trigger is bigger number and echo is smaller
+HCSR04  frontCenter(p26, p9);
+HCSR04  frontRight(p26, p8);
+HCSR04  frontLeft(p26, p7);
+HCSR04  rear(p26, p6);
+
+const unsigned int waitDuration = 55; //ms
+const unsigned int MaxDistance = 40;
+
+unsigned int sensorDistance[4] = {9999, 9999 ,9999, 9999};
+
+void getDistance(){
+    for(int i = 0; i<4; ++i)
+        sensorDistance[i] = 9999;
+
+    frontRight.start();
+
+    wait_ms(waitDuration);
+
+    sensorDistance[0] = frontCenter.get_dist_cm();
+    sensorDistance[1] = frontRight.get_dist_cm();
+    sensorDistance[2] = frontLeft.get_dist_cm();
+    sensorDistance[3] = rear.get_dist_cm();
+}
+
+void printDistance(){
+    const string SENSOR[] = {"CENTER", "RIGHT", "LEFT", "REAR"};
+    
+    for(int i = 0; i<4; ++i)
+        pc.printf("%s: %dcm\r\n", SENSOR[i] ,sensorDistance[i]);
+    
+    pc.printf("\n\r");
+}
+
+void printDistance2(int maxSensor){
+    const string SENSOR[] = {"CENTER", "RIGHT", "LEFT", "REAR"};
+    if(maxSensor>3)
+        maxSensor = 3;
+        
+    for(int i = 0; i<maxSensor; ++i)
+        pc.printf("%s: %dcm\r\n", SENSOR[i] ,sensorDistance[i]);
+    pc.printf("\n\r");    
+}
+
+//return true if obstacle is to close and car needs to dodge
+bool avoidObstacle(const unsigned int &distance){
+    return distance<MaxDistance;
+}
+
+#endif //SONAR_SENSOR_SONAR_H
\ No newline at end of file