Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 #include "Plotting.h" 00003 #include "Motor.h" 00004 #include "HallSensor.h" 00005 #include "SpeedController.h" 00006 00007 //To plot with usb, set as below. 00008 Serial pc(USBTX,USBRX); // Tx, Rx Pin 00009 Plotting plot; 00010 00011 //Set each gpio to see the output of the hall sensor as a led 00012 DigitalOut myled1(LED1); 00013 HallSensor hall (p29); 00014 Motor motor (p23, p24, p25, p26); 00015 PIDController speedController(0.1, 0.5, 0.0, 100.0, 0.0, -1.0, 100.0);//0.05514 00016 00017 //Interrupt is generated every 1ms and count is increased by 1 00018 unsigned int uiFlag_1ms = 0; 00019 unsigned int uiFlag_50ms = 0; 00020 00021 void counter_1ms () 00022 { 00023 uiFlag_1ms++; 00024 uiFlag_50ms++; 00025 } 00026 00027 float myabs(float a) 00028 { 00029 if(a<0.0) { 00030 a*=-1.0; 00031 } 00032 return a; 00033 } 00034 00035 00036 int main() 00037 { 00038 wait(1); 00039 00040 //Set the 1ms thicker. 00041 Ticker ticker_1ms; 00042 ticker_1ms.attach (&counter_1ms, 0.001); 00043 00044 // for calculating dt 00045 Timer time; 00046 time.start(); 00047 00048 float curr_speed = 0.0; 00049 float u_percent= 0.0; 00050 float setSpeed=0.0; 00051 00052 00053 while(1) { 00054 // Every 1 ms, 00055 if(uiFlag_1ms >= 1) { 00056 uiFlag_1ms = 0; 00057 00058 //If A is 0, turn on the LED. 00059 myled1 = hall.getPinState(); 00060 00061 00062 // Speed control 00063 setSpeed = 550.0; 00064 speedController.setTarget (setSpeed); 00065 curr_speed = hall.getSpeed_rps(); 00066 u_percent = speedController.update (curr_speed, time.read()); 00067 motor.setSpeed_percent(u_percent, FORWARD); 00068 } 00069 00070 // Every 50 ms, 00071 if(uiFlag_50ms >= 50) { 00072 uiFlag_50ms = 0; 00073 00074 // clear plotting buffer 00075 plot.reset (); 00076 00077 // put data to buffer 00078 plot.put (hall.getSpeed_rps(), 0); 00079 00080 // send buffer 00081 plot.send (&pc); 00082 } 00083 } 00084 }
Generated on Wed Jul 27 2022 20:12:06 by
1.7.2