Assignment 2c commit work

Dependencies:   MODSERIAL mbed

Fork of Minor_test_serial by First Last

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "MODSERIAL.h"
00003 
00004 DigitalOut directionpin1(D4);
00005 PwmOut pwmpin1(D5);
00006 AnalogIn potmetervalue1(A1);
00007 DigitalIn button2(D9);          //klopt dit?
00008 
00009 DigitalOut directionpin2(D7);
00010 PwmOut pwmpin2(D6);
00011 AnalogIn potmetervalue2(A2);
00012 DigitalIn button1(D10);          //klopt dit?
00013 
00014 MODSERIAL pc(USBTX, USBRX);
00015 
00016 int main()
00017 { 
00018     pc.baud(115200);
00019     pc.printf("hello\n\r");
00020     pwmpin1.period_us(60); //60 microseconds PWM period, 16.7 kHz 
00021 
00022     
00023     while (true)
00024     {
00025           
00026           float u1 = potmetervalue1;
00027           float u2 = potmetervalue2;
00028           
00029           float m1 = ((u1*2.0f)-1.0f);
00030           float m2 = ((u2*2.0f)-1.0f);
00031           
00032          // pc.printf("Motor value is %f\n\r",m);
00033 
00034           /*if (m <= -0.005) 
00035             {
00036                 directionpin.write(0);
00037                 pwmpin = fabs(m); //pwm duty cycle can only be positive, floating 
00038                 pwmpin.period_us(60); //60 microseconds PWM period, 16.7 kHz 
00039                 pc.printf("Motor draait naar A\n\r");
00040             }
00041             
00042           else if (m >= 0.005)
00043             { 
00044                 directionpin.write(1);
00045                 pwmpin = fabs(m);
00046                 pwmpin.period_us(60); //60 microseconds PWM period, 16.7 kHz
00047                 pc.printf("Motor draait naar B\n\r");
00048 
00049             }
00050         
00051           else if (-0.4 < m && m < 0.)
00052             {
00053                 //pwmpin = 0.0;
00054                 pwmpin.period_us(60); //60 microseconds PWM period, 16.7 kHz
00055                 pc.printf("Motor zou moeten stoppen.\n\r");
00056             }
00057             
00058             Met deze code kunnen we serial communication gebruiken. Als we dit niet nodig hebben en alleen de motor willen aansturen, dan gebruiken we de code hieronder.
00059             */
00060         
00061         pwmpin1 = fabs(m1*0.6f)+0.4f;     //pwm duty cycle can only be positive, floating, 0.4f is "inefficiënt", dit tellen we erbij op, en keer 0.6 om te corrigeren voor de helling.
00062         directionpin1.write(m1>0);        //Indien waar, motor draait rechtsom. Indien niet waar, motor draait linksom. 
00063         wait(0.001f);                   //zodat de code niet oneindig doorgaat.
00064 
00065         pwmpin2 = fabs(m2*0.6f)+0.4f;    
00066         directionpin2.write(m2>0);        
00067         wait(0.001f);
00068             
00069     }
00070 }
00071