test

Dependencies:   RemoteIR mbed

Files at this revision

API Documentation at this revision

Comitter:
kolanery
Date:
Sat May 13 19:42:23 2017 +0000
Parent:
5:5d34a8feffe2
Commit message:
update test case

Changed in this revision

encoder.cpp Show annotated file Show diff for this revision Revisions of this file
encoder.h Show annotated file Show diff for this revision Revisions of this file
left_motor.cpp Show annotated file Show diff for this revision Revisions of this file
left_motor.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
motor.cpp Show annotated file Show diff for this revision Revisions of this file
motor.h Show annotated file Show diff for this revision Revisions of this file
--- a/encoder.cpp	Tue May 09 01:21:47 2017 +0000
+++ b/encoder.cpp	Sat May 13 19:42:23 2017 +0000
@@ -1,21 +1,15 @@
+/*
 #include "encoder.h"
-/*
-Reset both encoders
-*/
+
+
 void resetEncoders(){
     leftEncoder.reset();
 }
 
-/*
-Returns the average number of pulses across both encoders since last reset. Unit is encoder pulses; intended for straight driving only.
-*/
 int getEncoderDistance(){
     return (leftEncoder) >> 1;
 }
 
-/*
- * Represents a quadrature motor encoder. Modified from mbed QEI 
- */
 Encoder::Encoder(PinName channelA, PinName channelB) : 
     channelA_(channelA), 
     channelB_(channelB){
@@ -58,4 +52,4 @@
 //Resets the encoder
 void Encoder::reset(void) {
     pulses = 0;
-}
+}*/
--- a/encoder.h	Tue May 09 01:21:47 2017 +0000
+++ b/encoder.h	Sat May 13 19:42:23 2017 +0000
@@ -1,15 +1,11 @@
+/*
 #ifndef ENCODER_H
 #define ENCODER_H
 #include "mbed.h"
 
-/*
-* Reset both encoders
-*/
+
 void resetEncoders();
 
-/*
-Returns the average number of pulses across both encoders since last reset. Unit is encoder pulses; intended for straight driving only.
-*/
 int getEncoderDistance();
 
 
@@ -35,7 +31,7 @@
     
     void encode(void);
 };
-
+*/
 
 extern Encoder leftEncoder;
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/left_motor.cpp	Sat May 13 19:42:23 2017 +0000
@@ -0,0 +1,32 @@
+#include "left_motor.h"
+const int ONE_MILLISECOND = 0.001;
+PwmOut motor_left_sig(PB_7);
+DigitalOut dir_left(PB_6);
+
+
+LeftMotor::LeftMotor() {
+    curr_speed = 0;
+    dir_left = 1;
+}
+
+//Sets motor speed
+void LeftMotor::speed(float speed) {
+    curr_speed = speed;
+    motor_left_sig.write(speed);
+}
+
+void LeftMotor::inv_dir(bool dir){
+    if(dir == 1)
+        dir_left = 1;
+    else
+        dir_left = 0;
+}
+
+void LeftMotor::set_period(float period) {
+    motor_left_sig.period(period);
+}
+
+//Sets motor speed to 0
+void LeftMotor::stop() {
+    speed(0);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/left_motor.h	Sat May 13 19:42:23 2017 +0000
@@ -0,0 +1,31 @@
+#ifndef LEFT_MOTOR_H
+#define LEFT_MOTOR_H
+#include "mbed.h"
+class LeftMotor {
+public:
+    LeftMotor();
+    
+    // Define speed as float value between -1.0 and 1.0
+    void speed(float speed);
+    
+    void inv_dir(bool dir);
+    // Stop the motor without breaking mechanism
+    void stop(void);
+    
+    void set_period(float period);
+    
+    // Override operator for setting motor speed
+    void operator = (float speed) {
+        LeftMotor::speed(speed);
+    }
+    
+    // Getter method to retrieve the current speed
+    float read(){
+        return curr_speed;
+    }
+    
+private:
+    float curr_speed;
+};
+
+#endif
\ No newline at end of file
--- a/main.cpp	Tue May 09 01:21:47 2017 +0000
+++ b/main.cpp	Sat May 13 19:42:23 2017 +0000
@@ -1,100 +1,193 @@
-/**
-* This is simple program to test usart with IR sensors.
-*/
 #include "mbed.h"
-#include "IRSensor.h"
-#include "encoder.h"
-//#include "motor.h"
- Serial serial (PA_9, PA_10);
+//#include "IRSensor.h"
+//#include "encoder.h"
+#include "left_motor.h"
+
+/*
+Serial serial (PA_9, PA_10);
+
 IRSensor testIR_RIGHT(PB_0, PA_4); //emmiter and receiver right most sensor
 IRSensor testIR_LEFT(PA_8, PC_5); //emmiter and receiver left most sensor
 IRSensor testIR_TOP1(PC_6, PA_7); //emmiter and receiver TOP LEFT sensor
 IRSensor testIR_TOP2(PB_10, PA_6); //emmiter and receiver TOP RIGHT sensor
 IRSensor testIR_DIAG_LEFT(PC_7, PC_4); //emmiter and receiver DIAG LEFT sensor
 IRSensor testIR_DIAG_RIGHT(PB_1, PA_5); //emmiter and receiver DIAG RIGHTsensor
- DigitalOut testLed(PB_12);
- DigitalOut testLed2(PB_13);
- DigitalOut testLed3(PB_14);
- DigitalOut testLed4(PB_15);
- AnalogIn Ain(PA_3);
- AnalogIn gyro(PC_1);
- DigitalOut gyro_sleep(PC_2);
+*/
+
+
+DigitalOut testLed(PB_12);
+DigitalOut testLed2(PB_13);
+DigitalOut testLed3(PB_14);
+DigitalOut testLed4(PB_15);
+
+/*
+AnalogIn Ain(PA_3);
+AnalogIn gyro(PC_1);
+DigitalOut gyro_sleep(PC_2);
+
  
+*/
+
 //Motor leftMotor(PB_7, PB_6);
 //Motor rightMotor(PB_9, PB_8);
+/*
+
+
+
+*/
+
 // pwm
+/*
 PwmOut motor_pwm_left_sig(PB_7);
 DigitalOut dir_left(PB_6);
 //// dir
 PwmOut motor_pwm_right_sig(PB_9);
 DigitalOut dir_right(PB_8);
-
-
+*/
+/*
 
 Encoder leftEncoder(PC_9, PC_8);
  
- int main ()
- {
-     serial.printf ("Start Program\n");
-     //leftMotor = 0.1f;
-     
-
-//         motor_pwm_left_sig.period(0.001f); 
-//         motor_pwm_left_sig.write(0.2f);
-//         motor_pwm_right_sig.period(0.001f); 
-//         motor_pwm_right_sig.write(0.6f);
-//         dir_left = 0;
-//         dir_right = 1;
-//         
-//         wait(7);
-//         motor_pwm_right_sig = 0;
-//         dir_right = 0;
-//         motor_pwm_left_sig = 0;
-         
-
-     //double angle 
-     
-     while (1)
-     {
-        testLed2 = 1; 
-        testLed3 = 1;
-         wait (0.2);
-         testLed2 = 0;
-         testLed3 = 0; 
-         wait (0.2); 
-         
-//         serial.printf("Left sensor: %f\r\n", testIR_LEFT.readIR());
-//         serial.printf("Left Diagnal %f\r\n", testIR_DIAG_LEFT.readIR());
-         serial.printf("TOP LEFT %f\r\n", testIR_TOP1.readIR());
-         serial.printf("TOP RIGHT %f\r\n", testIR_TOP2.readIR());
-//         serial.printf("Right Diagnal %f\r\n", testIR_DIAG_RIGHT.readIR());
-//         serial.printf("Right sensor: %f\r\n", testIR_RIGHT.readIR());
-         wait(1);
-         //______________TEST GYRO
-        double offset = .4451;
-         gyro_sleep = 1;
-        double gyro_value = (gyro.read() - offset) * .67 / offset * 600;  // Offset of .4455 measured experimentally
-     //   angle += gyro_value * .01;
-        wait(.01);
-       
-        serial.printf("Gyro value: %f\r\n", gyro_value);
-         
-         
-         //______________
-         
-         
-         //serial.printf("voltage value is: %3.3f%%\r\n", Ain.read()*100.0f);
-         //serial.printf("normalized: 0x%04X \r\n", Ain.read_u16());
-
-         if(Ain.read() < 0.67f){
+ 
+void battery_helper() {
+    if(Ain.read() < 0.67f){
              testLed = 1;
              testLed2 = 1;
              testLed3 = 1;
              testLed4 = 1;
              serial.printf("voltage is in danger area, UNPLUG\r\n");
-         }
+    }
+} 
+*/
+
+/*
+void toggle() {
+    testLed2 = 1; 
+    testLed3 = 1;
+    
+    leftMotor.speed(0.2f);
+    rightMotor.speed(0.2f);
+    
+    
+    motor_pwm_left_sig.period(0.001f); 
+    motor_pwm_left_sig.write(0.2f);
+    motor_pwm_right_sig.period(0.001f); 
+    motor_pwm_right_sig.write(0.6f);
+    
+    wait(5);
+    motor_pwm_right_sig = 0;
+    motor_pwm_left_sig = 0;
+    
+ 
+} */
+ 
+int main ()
+{
+    //serial.printf ("Start Program\n");
+    testLed2 = 1; 
+    testLed3 = 1;
+    
+    LeftMotor * motor = new LeftMotor();
+    motor->inv_dir(0);
+    motor->speed(0.9f);
+    
+    
+    /*
+    
+    PwmOut motor_pwm_left_sig(PB_7);
+    DigitalOut dir_left(PB_6);
+    //// dir
+    PwmOut motor_pwm_right_sig(PB_9);
+    DigitalOut dir_right(PB_8);
+    
+    */
+    
+    /*
+    Motor * leftMotor = new Motor(PB_7, PB_6);
+    Motor * rightMotor = new Motor(PB_9, PB_8);
+    
+    leftMotor->speed(0.2f);
+    rightMotor->speed(0.2f);
+    */
+    
+    //leftMotor = 0.2f;
+    //rightMotor = 0.2f;
+        
+    /* 
+     wait(3);
+     toggle();
+     
+     wait(3);
+     toggle();
+     
+     wait(3);
+     toggle();
+     
+     wait(3);
+     toggle();
+     */
+     
+     //double angle 
+     /*
+     while (1)
+     {
+        battery_helper();
+        
+        testLed2 = 1; 
+        testLed3 = 1;
+         
+        // wait (0.2);
+        // testLed2 = 0;
+        // testLed3 = 0; 
+         //wait (0.2); 
+         
+
+         motor_pwm_left_sig.period(0.001f); 
+         motor_pwm_left_sig.write(0.2f);
+         motor_pwm_right_sig.period(0.001f); 
+         motor_pwm_right_sig.write(0.6f);
+         dir_left = 0;
+         dir_right = 1;
+         
+         wait(2);
+         
+         motor_pwm_right_sig = 0;
+         dir_right = 0;
+         motor_pwm_left_sig = 0;
+         
        
          int dist = getEncoderDistance();
          //serial.printf("Encoder %d \r\n", getEncoderDistance());        
-     }
- } 
\ No newline at end of file
+     } */
+ }
+ 
+
+ /*
+ void motor_tester() {
+
+ } 
+
+ void sensor_tester() {
+     //         serial.printf("Left sensor: %f\r\n", testIR_LEFT.readIR());
+    //         serial.printf("Left Diagnal %f\r\n", testIR_DIAG_LEFT.readIR());
+         serial.printf("TOP LEFT %f\r\n", testIR_TOP1.readIR());
+         serial.printf("TOP RIGHT %f\r\n", testIR_TOP2.readIR());
+    //         serial.printf("Right Diagnal %f\r\n", testIR_DIAG_RIGHT.readIR());
+    //         serial.printf("Right sensor: %f\r\n", testIR_RIGHT.readIR());
+         wait(1);
+         
+ }
+ 
+ void voltage_tester() {
+    serial.printf("voltage value is: %3.3f%%\r\n", Ain.read()*100.0f);
+    serial.printf("normalized: 0x%04X \r\n", Ain.read_u16());
+ } 
+ 
+ void gyro_tester() {
+    double offset = .4451;
+    gyro_sleep = 1;
+    double gyro_value = (gyro.read() - offset) * .67 / offset * 600;  // Offset of .4455 measured experimentally
+    angle += gyro_value * .01;
+    wait(.01);
+    serial.printf("Gyro value: %f\r\n", gyro_value);
+ } */
\ No newline at end of file
--- a/motor.cpp	Tue May 09 01:21:47 2017 +0000
+++ b/motor.cpp	Sat May 13 19:42:23 2017 +0000
@@ -1,31 +1,28 @@
+/*
 #include "motor.h"
-const int FORWARD = 1;
-const int BACKWARD = 0;
+#include "mbed.h"
 const int ONE_MILLISECOND = 0.001;
 
-Motor::Motor(PinName _pwm_pin, PinName _dir):
+Motor::Motor(PinName curr_pwm_pin, PinName curr_dir):
         pwm_pin(_pwm_pin), dir(_dir){
+    pwm_pin(curr_pwm_pin);
+    dir(curr_dir);
+    
     pwm_pin.period(ONE_MILLISECOND);
-    pwm_pin = 0; 
+    pwm_pin.write(0.1f); 
     dir = 0;
     curr_speed = 0;
 }
 
 //Sets motor speed
 void Motor::speed(float speed) {
-    if (speed < 0.0f){ //Backwards
-        if (speed < -1.0f){
-            speed = -1.0f;
-        }
-        dir = FORWARD;
-        pwm_pin = curr_speed = speed + 1.0f; // Inverts it so 1 is off and 0 is on
-    } else { //Forwards   
-        dir = BACKWARD;
-        pwm_pin = curr_speed = speed;
-    }
+    curr_speed = speed;
+    pwm_pin.write(speed);
 }
 
 //Sets motor speed to 0
 void Motor::stop() {
     speed(0);
-}
\ No newline at end of file
+}
+
+*/
\ No newline at end of file
--- a/motor.h	Tue May 09 01:21:47 2017 +0000
+++ b/motor.h	Sat May 13 19:42:23 2017 +0000
@@ -23,13 +23,9 @@
     }
     
 private:
-    volatile float curr_speed;
+    float curr_speed;
     PwmOut pwm_pin;
     DigitalOut dir;
 };
 
-// Declaring as extern to enable global scope
-extern Motor leftMotor;
-extern Motor rightMotor;
-
 #endif
\ No newline at end of file