main.cpp: Sensoren einlesen und Motoren ansteuern

Dependencies:   mbed

main.cpp

Committer:
Helvis
Date:
2018-04-10
Revision:
1:1adf5dfcc7bb
Parent:
0:9a3e7847a4be
Child:
2:4ccdc62022c2

File content as of revision 1:1adf5dfcc7bb:

#include <mbed.h>

#include "EncoderCounter.h"
#include "Controller.h"
#include "IRSensor.h"
#include "Motion.h"

//User Button
    DigitalIn button(PC_13);

//Sensors:
    
    AnalogIn lineSensor(PA_0);
    AnalogIn distanceL(PA_1);
    AnalogIn distanceC(PA_4);
    AnalogIn distanceR(PB_0);

    IRSensor irSensorL (distanceL);
    IRSensor irSensorC (distanceC);
    IRSensor irSensorR (distanceR);

//Motors:
    DigitalOut myled(LED1);
    
    DigitalOut enableMotorDriver(PB_2);
    
    DigitalIn motorDriverFault(PB_14);
    DigitalIn motorDriverWarning(PB_15);
    
    PwmOut pwmLeft(PA_8);
    PwmOut pwmRight(PA_9);
    
    EncoderCounter counterLeft(PB_6, PB_7);
    EncoderCounter counterRight(PA_6, PC_7);
    
    Controller controller(pwmLeft, pwmRight, counterLeft, counterRight);
    
    Motion motion(controller, counterLeft, counterRight, irSensorL, irSensorC, irSensorR);

int main() {
    
/** Micromouse Test 

    für den jeweiligen Test 0 im while Argument auf 1 setzen
*/
    
    //User Button einlesen: start Signal
    int start;
    short countsLeft = 0;
    short countsRight = 0;
    
    if (button.read() || start != 1) start = 1;
    else if(button.read() || start == 1) start = 0;


    /*
    Sensoren Test
    */
    while(start && 1) {
        
        float distanceL = irSensorL.read();
        float distanceC = irSensorC.read();
        float distanceR = irSensorR.read();
        printf("Rechts: %f  Mitte: %f  Links: %f\n", distanceL, distanceC, distanceR);
        
        wait(0.5f);
    }
    
    /*
    Motoren Test: Gerade Bewegung
    */
    while(start && 0) {
        
        controller.setDesiredSpeedLeft(50.0f); //Drehzahl in [rpm]
        controller.setDesiredSpeedRight(50.0f);
        enableMotorDriver = 1; //Schaltet  den Leistungstreiber ein
            while(1) {
        
                myled =! myled;
                wait(0.1f); //200 ms
                
                countsLeft = counterLeft.read();
                countsRight = counterRight.read();
                printf("countsLeft: %hd countsRight: %hd\n", countsLeft, countsRight);
            }     
    }
    
    /*
    Motoren Test: 180° Rotation
    */
        
    while(start && 0) {
        
        while(countsLeft  < 200 || countsRight > -200) {
            
            countsLeft = counterLeft.read();
            countsRight = counterRight.read();
        
            controller.setDesiredSpeedLeft(50.0f);
            controller.setDesiredSpeedRight(-50.0f);
        }
        
        controller.setDesiredSpeedLeft(0.0f);
        controller.setDesiredSpeedRight(0.0f); 
        
    }


/* Benötigte Ticks für 180* Rotation am Ort:

    RotationsDistanz Rd= DistanzRäder*pi/2
    
    UmfangRad = Drad*pi
    
    mm pro Count = UmfangRad/Counts pro Drehung
    
    Counts = Rd/mm pro Count
    
    => Counts pro Drehung rausfinden */
    while(start && 0) {
        
        while(countsLeft  < 100 || countsRight < 100) {
            
            countsLeft = counterLeft.read();
            countsRight = counterRight.read();
        
            controller.setDesiredSpeedLeft(50.0f);
            controller.setDesiredSpeedRight(50.0f);
        }
        
        controller.setDesiredSpeedLeft(0.0f);
        controller.setDesiredSpeedRight(0.0f); 
        
    }
}