Tripple Controller for the TLE5206 H Bridge motor controller
Embed:
(wiki syntax)
Show/hide line numbers
example1.h
00001 00002 #include "mbed.h" 00003 #include "SimpleTLE5206.h" 00004 00005 /* NOTE! The SimpleTLE5206 library supports the TLE5206 in 00006 * Mode 2 Sign/Magnitude Control using two PWM outputs to 00007 * control speed and direction. 00008 * 00009 * Pins that can be used are p21, p22, p23, p24, p25 and/or p26 00010 * in pairs. So the library supports upto 3 TLE5206 devices/motors. 00011 * 00012 * All PWM outputs use a common duty cycle. Therefore the third arg 00013 * to the constructor must be the same for all TLE5206 devices. To 00014 * ensure this, we use a #define DUTY_CYCLE_IN_HERTZ and supply it 00015 * to all instances of controllers created. 00016 * 00017 * Additionally you can use LED1, LED2, LED3 and.or LED4 as mimics. 00018 * However, if using:- 00019 * LED1 you cannot use p26 to drive an in to TLE5206 00020 * LED2 you cannot use p25 to drive an in to TLE5206 00021 * LED3 you cannot use p24 to drive an in to TLE5206 00022 * LED4 you cannot use p23 to drive an in to TLE5206 00023 * 00024 * The function SimpleTLE5206::setSpeed() takes a single arg, a double, 00025 * and should be in the range +1.0 to -1.0 where +1.0 is full speed in 00026 * a CW direction, -1.0 is full speed in a CCW direction and 0 is stopped. 00027 */ 00028 00029 #define DUTY_CYCLE_IN_HERTZ 50 00030 00031 // Create a motor "A", driven by a TLE5206 on pins 21 and 22 (attach scope first, not a motor!) 00032 SimpleTLE5206Output Ain1(p21); // TLE5206 In1 is connected to p21 00033 SimpleTLE5206Output Ain2(p22); // TLE5206 In2 is connected to p22 00034 SimpleTLE5206 motorA(&Ain1, &Ain2, DUTY_CYCLE_IN_HERTZ); // Create the TLE5206 controller. 00035 00036 // Create a motor "B", driven by a TLE5206 but on LEDs as a mimic. 00037 SimpleTLE5206Output Bin1(LED1); // TLE5206 In1 is connected to LED1 00038 SimpleTLE5206Output Bin2(LED2); // TLE5206 In2 is connected to LED2 00039 SimpleTLE5206 motorB(&Bin1, &Bin2, DUTY_CYCLE_IN_HERTZ); // Create the TLE5206 controller. 00040 00041 // Create a motor "C", driven by a TLE5206 but on LEDs as a mimic. 00042 SimpleTLE5206Output Cin1(LED3); // TLE5206 In1 is connected to LED3 00043 SimpleTLE5206Output Cin2(LED4); // TLE5206 In2 is connected to LED4 00044 SimpleTLE5206 motorC(&Cin1, &Cin2, DUTY_CYCLE_IN_HERTZ); // Create the TLE5206 controller. 00045 00046 Ticker A, B, C; 00047 00048 volatile double demand[3]; 00049 volatile double speed[3]; 00050 00051 #define PI 3.14159265 00052 00053 void Acallback(void) { 00054 speed[0] = sin(demand[0] * PI / 180.0); 00055 if (++demand[0] >= 360.0) demand[0] = 0.0; 00056 motorA.setSpeed(speed[0]); 00057 } 00058 00059 void Bcallback(void) { 00060 speed[1] = sin(demand[1] * PI / 180.0); 00061 if (++demand[1] >= 360.0) demand[1] = 0.0; 00062 motorB.setSpeed(speed[1]); 00063 } 00064 00065 void Ccallback(void) { 00066 speed[2] = sin(demand[2] * PI / 180.0); 00067 if (++demand[2] >= 360.0) demand[2] = 0.0; 00068 motorC.setSpeed(speed[2]); 00069 } 00070 00071 int main() { 00072 00073 volatile int trash = 0; 00074 00075 motorA.setSpeed(0); 00076 motorB.setSpeed(0); 00077 motorC.setSpeed(0); 00078 00079 // Init the global variables. 00080 for (int i = 0; i < 3; i++) { 00081 demand[i] = speed[i] = 0.0; 00082 } 00083 00084 // Note, you probably wouldn't want to move the speed of 00085 // a motor at this rate, may break it. This example uses 00086 // a high update rate (0.025) assuming you are attaching 00087 // an oscilloscope just for testing. It goes without saying 00088 // that the update rates for B and C are way to big, I just 00089 // choose these (0.005 and 0.0025) because it looks pretty 00090 // when used on LEDs! 00091 // Always use appropriate accel/decel rates when handling 00092 // motors/external hardware that moves. See example2.h for 00093 // a simple linear accel/decel profiler that does this. 00094 00095 A.attach(Acallback, 0.025); 00096 B.attach(Bcallback, 0.005); 00097 C.attach(Ccallback, 0.0025); 00098 00099 while(1) { 00100 /* The main loop has little to do as the Ticker callbacks 00101 set-up the speed changes for the example. So give it something 00102 to do. Maybe change this and use the spare time to calculate PI 00103 more accurately? Lol, just kidding. */ 00104 trash++; 00105 } 00106 } 00107 00108
Generated on Tue Jul 12 2022 21:37:52 by 1.7.2