Završni rad NXP Cup

Dependencies:   HBridgeDCMotor mbed FastPWM AutomationElements

main.cpp

Committer:
btomic
Date:
2016-06-03
Revision:
4:d3c4924179f0
Parent:
3:fc17c2ad6b81
Child:
5:d3bdc0672891

File content as of revision 4:d3c4924179f0:

#include "mbed.h"
#include "HBridgeDCMotor.h"
#include "FastPWM.h"

DigitalIn fault(PTE20);
DigitalIn sw1(PTC13);
DigitalIn sw2(PTC17);
AnalogIn a_feedback(PTE23);
AnalogIn b_feedback(PTE22);
AnalogIn pot1(PTB3);
AnalogIn pot2(PTB2);

Serial pc(PTA2, PTA1);


DigitalOut enable(PTE21);  
DigitalOut d1(PTB8); 
DigitalOut d2(PTB9);
DigitalOut d3(PTB10);
DigitalOut d4(PTB11);
//DigitalOut red(PTB18);
//DigitalOut green(PTB19);
//DigitalOut blue(PTD1);

//HBridgeDCMotor motor_a(PTC3, PTC4);
//HBridgeDCMotor motor_b(PTC1, PTC2);

PwmOut servo(PTB0);

FastPWM motor1(PTC3);
DigitalOut mot(PTC4);
FastPWM motor2(PTC1);
DigitalOut mot22(PTC2);



void steerServo(float angleDegrees){   
    float minPw = 0.0005, maxPw = 0.0025, minAng = -90, maxAng = 90, pulse = 0;
    pulse = minPw + ((maxPw - minPw)/(maxAng - minAng))*(angleDegrees - minAng);
    servo.pulsewidth(pulse);
    }

int main() {
    float x=0, pw =0, PWmin=-90, PWmax=90;
    float angleDegrees = 0;
    float sampleTime = 50e-3, switchingFrequency = 5000, rampTime = 3;
    servo.period(1.0/50);

    steerServo(angleDegrees);
    motor1.period_us(500);
    motor2.period_us(500);   
  //  motor_a.configure(sampleTime, switchingFrequency, rampTime, rampTime);
   // motor_b.configure(sampleTime, switchingFrequency, rampTime, rampTime);
    wait(5);

    while(1){
        x = pot1;
        angleDegrees = PWmin + (PWmax - PWmin) * pot1;
enable = 1;
       // if(sw1==1 && sw2==0) enable = sw1;
      //  else enable = 0;
        
        mot = 0;
        mot22 = 0;
        motor1.pulsewidth_us(100);

        motor2.pulsewidth_us(100);
            


       steerServo(angleDegrees);
       pc.printf("pot1: %f   angleDegrees: %f\r", x, angleDegrees);
        
    //   motor_a.setDutyCycle(0.2);
     //  motor_b.setDutyCycle(0.2); 
        
        if(x<0.501 && x>0.499) {d1=1; d2=1; d3=1; d4=1;}
        else {d1=1; d2=0; d3=0; d4=1;}      

        }
    enable = 0;
   // red = 1;
    

}