Library

Committer:
philip_17
Date:
Mon Jan 25 11:37:16 2016 +0000
Revision:
1:6af83dcf6691
Parent:
Pwm.cpp@0:04250620f3d9
Bertl

Who changed what in which revision?

UserRevisionLine numberNew contents of line
philip_17 0:04250620f3d9 1 #include "mbed.h"
philip_17 0:04250620f3d9 2 #include "Pwm.h"
philip_17 0:04250620f3d9 3 // Philip Pollheimer, 23.12.2015
philip_17 0:04250620f3d9 4 // Controlling the engines of the BERTL14 using PWM
philip_17 0:04250620f3d9 5
philip_17 0:04250620f3d9 6 // Declaration of outputs
philip_17 0:04250620f3d9 7
philip_17 0:04250620f3d9 8 DigitalOut ML_R(P1_0); // IN1 EP10, MG1A => MG1 Motor-Pin 2
philip_17 0:04250620f3d9 9 DigitalOut ML_F(P1_1); // IN2 EP10, MG1B => MG1 Motor-Pin 1
philip_17 0:04250620f3d9 10 PwmOut ML_EN(P1_15); // EN1, P34
philip_17 0:04250620f3d9 11
philip_17 0:04250620f3d9 12 DigitalOut MR_F(P1_3); // IN4 EP13, MG2A => MG2 Motor-Pin 2
philip_17 0:04250620f3d9 13 DigitalOut MR_R(P1_4); // IN3 EP14, MG2B => MG2 Motor-Pin 1
philip_17 0:04250620f3d9 14 PwmOut MR_EN(P0_21); // EN2, P36
philip_17 0:04250620f3d9 15
philip_17 0:04250620f3d9 16
philip_17 0:04250620f3d9 17 void bertl_motor(int links, int rechts) // subprogramm
philip_17 0:04250620f3d9 18 {
philip_17 0:04250620f3d9 19 float v_motor_rechts; // v => velocity
philip_17 0:04250620f3d9 20 float v_motor_links;
philip_17 0:04250620f3d9 21
philip_17 0:04250620f3d9 22 // if-conditions when parameters are bigger than 0 then the first formula will be used.
philip_17 0:04250620f3d9 23 // otherwise the otherone. Thats because if the parameter is negative the value will also be negative.
philip_17 0:04250620f3d9 24 // Therefore we write -255, the minus becomes a plus (-(-5) => +5)
philip_17 0:04250620f3d9 25
philip_17 0:04250620f3d9 26 if(links > 0) {
philip_17 0:04250620f3d9 27 v_motor_links = links / 255.0; // Formula for the PWM. The variable v_motor_[name] is always < 1, due to the PWM function.
philip_17 0:04250620f3d9 28 }
philip_17 0:04250620f3d9 29 if(rechts > 0){
philip_17 0:04250620f3d9 30 v_motor_rechts = rechts / 255.0;
philip_17 0:04250620f3d9 31 } // In PWM 1 equals 5V and 0 equals 0V. The formula calculates a value from 0 to 1 (which is converted to Volt)
philip_17 0:04250620f3d9 32 // and with this value the engine will be supplied.
philip_17 0:04250620f3d9 33 if(links < 0) {
philip_17 0:04250620f3d9 34 v_motor_links = links / -255.0;
philip_17 0:04250620f3d9 35 }
philip_17 0:04250620f3d9 36 if(rechts < 0){
philip_17 0:04250620f3d9 37 v_motor_rechts = rechts / -255.0;
philip_17 0:04250620f3d9 38
philip_17 0:04250620f3d9 39 }
philip_17 0:04250620f3d9 40
philip_17 0:04250620f3d9 41
philip_17 0:04250620f3d9 42 // if-conditions for positive parameters (when the transfered parameters are positive, the engine turns forward)
philip_17 0:04250620f3d9 43 if(links > 0 && rechts < links) {
philip_17 0:04250620f3d9 44
philip_17 0:04250620f3d9 45 ML_EN = v_motor_links;
philip_17 0:04250620f3d9 46 MR_EN = v_motor_rechts;
philip_17 0:04250620f3d9 47 if(rechts < 0)
philip_17 0:04250620f3d9 48 {
philip_17 0:04250620f3d9 49 ML_F = MR_R = 1;
philip_17 0:04250620f3d9 50 wait(1.0);
philip_17 0:04250620f3d9 51 ML_F = MR_R = 0;
philip_17 0:04250620f3d9 52 }
philip_17 0:04250620f3d9 53 ML_F = MR_F = 1;
philip_17 0:04250620f3d9 54 wait(4.0);
philip_17 0:04250620f3d9 55 ML_F = MR_F = 0;
philip_17 0:04250620f3d9 56 }
philip_17 0:04250620f3d9 57 if(rechts > 0 && rechts > links) {
philip_17 0:04250620f3d9 58
philip_17 0:04250620f3d9 59 ML_EN = v_motor_links;
philip_17 0:04250620f3d9 60 MR_EN = v_motor_rechts;
philip_17 0:04250620f3d9 61 if(links < 0)
philip_17 0:04250620f3d9 62 {
philip_17 0:04250620f3d9 63 ML_R = MR_F = 1;
philip_17 0:04250620f3d9 64 wait(1.0);
philip_17 0:04250620f3d9 65 ML_R = MR_F = 0;
philip_17 0:04250620f3d9 66
philip_17 0:04250620f3d9 67 }
philip_17 0:04250620f3d9 68 ML_F = MR_F = 1;
philip_17 0:04250620f3d9 69 wait(4.0);
philip_17 0:04250620f3d9 70 ML_F = MR_F = 0;
philip_17 0:04250620f3d9 71 }
philip_17 0:04250620f3d9 72 // if-conditions for negative parameters(when the transfered parameters are negative, the engine turns reverse)
philip_17 0:04250620f3d9 73 if(links < 0 && rechts > links) {
philip_17 0:04250620f3d9 74
philip_17 0:04250620f3d9 75 ML_EN = v_motor_links;
philip_17 0:04250620f3d9 76 MR_EN = v_motor_rechts;
philip_17 0:04250620f3d9 77 ML_R = MR_R = 1;
philip_17 0:04250620f3d9 78 wait(4.0);
philip_17 0:04250620f3d9 79 ML_R = MR_R = 0;
philip_17 0:04250620f3d9 80 }
philip_17 0:04250620f3d9 81 if(rechts < 0 && rechts < links) {
philip_17 0:04250620f3d9 82
philip_17 0:04250620f3d9 83 ML_EN = v_motor_links;
philip_17 0:04250620f3d9 84 MR_EN = v_motor_rechts;
philip_17 0:04250620f3d9 85 ML_R = MR_R = 1;
philip_17 0:04250620f3d9 86 wait(4.0);
philip_17 0:04250620f3d9 87 ML_R = MR_R = 0;
philip_17 0:04250620f3d9 88 }
philip_17 0:04250620f3d9 89 }