Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed Servo ServoArm
Fork of PES_Yanick by
Revision 0:d267b248eff4, committed 2017-03-11
- Comitter:
- beacon
- Date:
- Sat Mar 11 10:14:00 2017 +0000
- Child:
- 1:388c915756f5
- Commit message:
- l
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Headers/Robot.h Sat Mar 11 10:14:00 2017 +0000
@@ -0,0 +1,67 @@
+#ifndef ROBOT_H
+#define ROBOT_H
+
+#include <cstdlib>
+#include <mbed.h>
+
+#include "mbed.h"
+
+
+/* Declarations for the Motors in the Robot in order to drive -------- */
+class Robot
+{
+
+ public:
+
+ //Robot related:
+ Robot(PwmOut* left, PwmOut* right, DigitalOut* enable);
+ void drive();
+ void turnLeft();
+ void turnRight();
+ void turnAround(int left);
+
+ //DistanceSensors related:
+ class DistanceSensors
+ {
+ public:
+
+ DistanceSensors(AnalogIn* sensorVoltage, DigitalOut* bit0, DigitalOut* bit1, DigitalOut* bit2, int number);
+ DistanceSensors();
+
+ void init(AnalogIn* sensorVoltage, DigitalOut* bit0, DigitalOut* bit1, DigitalOut* bit2, int number);
+ virtual ~DistanceSensors();
+ float read();
+
+ operator float(); //Is here to constantly return the value of read.
+
+ private:
+
+ AnalogIn* sensorVoltage;
+ DigitalOut* bit0;
+ DigitalOut* bit1;
+ DigitalOut* bit2;
+
+ int number;
+
+ };
+
+
+ DistanceSensors sensors[6];
+ void init();
+
+ private:
+
+ //Robot related:
+ PwmOut* left;
+ PwmOut* right;
+
+ DigitalOut* powerSignal;
+ DigitalIn* errorSignal;
+ DigitalIn* overtemperatur;
+
+ //Distance sensors related:
+ //DistanceSensors sensors[6];
+
+};
+
+#endif
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Sources/DistanceSensors.cpp Sat Mar 11 10:14:00 2017 +0000
@@ -0,0 +1,46 @@
+#include <cmath>
+#include "Robot.h"
+
+
+
+Robot::DistanceSensors::DistanceSensors()
+{
+ //Nothing
+}
+
+Robot::DistanceSensors::DistanceSensors(AnalogIn* sensorVoltage, DigitalOut* bit0, DigitalOut* bit1, DigitalOut* bit2, int number)
+{
+ init(sensorVoltage, bit0, bit1, bit2, number);
+}
+
+//initialise
+void Robot::DistanceSensors::init(AnalogIn* sensorVoltage, DigitalOut* bit0, DigitalOut* bit1, DigitalOut* bit2, int number)
+{
+ this->sensorVoltage = sensorVoltage;
+ this->bit0 = bit0;
+ this->bit1 = bit1;
+ this->bit2 = bit2;
+}
+
+
+Robot::DistanceSensors::~DistanceSensors()
+{
+}
+
+
+float Robot::DistanceSensors::read()//Return the distance of an object
+{
+ *bit0 = number & 1; // Set the first bit of the Sensors MUX
+ *bit1 = number & 2; // Set the second bit of the Sensors MUX
+ *bit2 = number & 4; // Set the third bit of the Sensors MUX
+
+ float Usensor=sensorVoltage->read(); //Read the Voltage from the selected distance sensor
+ float Distance=-0.38f*sqrt(Usensor)+0.38f;
+ return Distance;
+}
+
+Robot::DistanceSensors::operator float()
+{
+ return read();
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Sources/Robot.cpp Sat Mar 11 10:14:00 2017 +0000
@@ -0,0 +1,62 @@
+#include "Robot.h"
+
+/* Work in progress -------------------------------------------- */
+
+Robot::Robot(PwmOut* left, PwmOut* right, DigitalOut* enableSignal)
+{
+ this->powerSignal = enableSignal;
+ this->left = left;
+ this->right = right;
+
+ this->left->period(0.00005f);
+ this->right->period(0.00005f);
+}
+
+/* work in progress ------------------------------------------- */
+void Robot::drive()
+{
+ //pwm determine what direction it goes.
+ *powerSignal = 1;
+ *left= 0.8f;
+ *right= 0.7f;
+ *left= 0.7f;
+ *right= 0.8f;
+}
+
+void Robot::turnLeft(){
+ *powerSignal = 1;
+ *left= 0.35f;
+ *right= 0.65f;
+
+}
+
+void Robot::turnRight(){
+ *powerSignal = 1;
+ *left= 0.65f;
+ *right= 0.35f;
+}
+
+void Robot::turnAround(int left){
+ *powerSignal = 1;
+
+ if (left){
+ turnLeft();
+ }
+
+ else{
+ turnRight();
+ }
+}
+
+//void Robot::init(){
+// Robot.DistanceSensors.init(&sensorVoltage, &bit0, &bit1, &bit2, ii);
+//}
+
+//Remember to set
+//DigitalOut powerMotor(PB_2) = 1;
+//DigitalIn errorMotor(PB_14);
+//if (errorMotor){
+//reset
+//}
+
+//Add management for Overpower!! Pin PB_15
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Sources/main.cpp Sat Mar 11 10:14:00 2017 +0000
@@ -0,0 +1,105 @@
+#include "mbed.h"
+#include "Robot.h"
+
+#include <cstdlib>
+
+#define NEAR 0.1f //Used for distance Sensors. If they're to near to a wall -> turn
+#define LEFT //Arrayindex of the left Sensor
+#define FWD //Arrayindex of the forward Sensor
+#define RIGHT //Arrayindex of the right Sensor
+
+
+//Robot related:
+PwmOut left(PA_8);
+PwmOut right(PA_3);
+
+DigitalOut powerSignal(PB_2);
+DigitalIn errorSignal(PB_14);
+DigitalIn overtemperatur(PB_15);
+
+Robot bot( &left, &right, &powerSignal );
+
+//DistanceSensors related:
+Serial pc(SERIAL_TX, SERIAL_RX);
+
+AnalogIn sensorVoltage(PB_1);
+DigitalOut enable(PC_1);
+DigitalOut bit0(PH_1);
+DigitalOut bit1(PC_2);
+DigitalOut bit2(PC_3);
+//Robot::DistanceSensors sensors[6];
+
+//LED related:
+DigitalOut leds[] = { PC_8, PC_6, PB_12, PA_7, PC_0, PC_9 };
+
+void initialiseDistanceSensors(){
+ for( int ii = 0; ii<6; ++ii) {
+ bot.sensors[ii].init(&sensorVoltage, &bit0, &bit1, &bit2, ii);
+
+ enable = 1;
+ }
+}
+
+void fahralgorithmus(){
+
+ int rando = 0; //Will be used for rdandom actions.
+
+ while( 1 ) { //Remember to change the indices. Also make into a seperate function
+
+ if ( bot.sensors[0] < NEAR ) { //Left Sensor
+ while ( bot.sensors[0] < NEAR ) {
+ bot.turnRight();
+ }
+ }
+
+ else if ( bot.sensors[1] < NEAR ) { //Front Sensor
+ rando = rand() % 2;
+ while ( bot.sensors[1] < NEAR ) {
+ bot.turnAround(rando);
+ }
+ }
+
+ else if ( bot.sensors[2] < NEAR ) { //Right Sensor
+ while ( bot.sensors[2] < NEAR ) {
+ bot.turnLeft();
+ }
+ }
+
+ else {
+ bot.drive();
+ }
+
+ }
+}
+
+/* * /
+
+
+int main(){
+ while( 1 ){
+ bot.drive();
+ }
+ return 0;
+}
+/* */
+
+
+/* */
+int main()
+{
+ initialiseDistanceSensors();
+
+ while( 1 ){
+ for(int i=0; i<6; i++){
+ if(bot.sensors[i] > 0.05f){
+ leds[i] = 1;
+ }
+ else{
+ leds[i] = 0;
+ }
+ }
+ }
+
+ return 0;
+}
+/* */
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sat Mar 11 10:14:00 2017 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/e1686b8d5b90 \ No newline at end of file
