Trapezoid drive Hall sensor

Dependencies:   mbed

Committer:
oguro
Date:
Mon Nov 16 01:12:50 2020 +0000
Revision:
0:8e42db611280
Trapezoid Drive with Hall Sensor

Who changed what in which revision?

UserRevisionLine numberNew contents of line
oguro 0:8e42db611280 1 #include "mbed.h"
oguro 0:8e42db611280 2
oguro 0:8e42db611280 3
oguro 0:8e42db611280 4 unsigned int q=0,r=0,s=0,START=8;
oguro 0:8e42db611280 5
oguro 0:8e42db611280 6 PwmOut mypwmA(PA_8); //PWM_OUT
oguro 0:8e42db611280 7 PwmOut mypwmB(PA_9); //PWM_OUT
oguro 0:8e42db611280 8 PwmOut mypwmC(PA_10);//PWM_OUT
oguro 0:8e42db611280 9
oguro 0:8e42db611280 10
oguro 0:8e42db611280 11 DigitalOut EN1(PC_10);
oguro 0:8e42db611280 12 DigitalOut EN2(PC_11);
oguro 0:8e42db611280 13 DigitalOut EN3(PC_12);
oguro 0:8e42db611280 14
oguro 0:8e42db611280 15 InterruptIn HA(PA_15);
oguro 0:8e42db611280 16 InterruptIn HB(PB_3);
oguro 0:8e42db611280 17 InterruptIn HC(PB_10);
oguro 0:8e42db611280 18
oguro 0:8e42db611280 19 unsigned int UP,VP,WP;
oguro 0:8e42db611280 20 AnalogIn V_adc(PC_2); // gaibu Volume
oguro 0:8e42db611280 21 //AnalogIn V_adc(PB_1); //Volume
oguro 0:8e42db611280 22
oguro 0:8e42db611280 23
oguro 0:8e42db611280 24 Serial pc(USBTX,USBRX);
oguro 0:8e42db611280 25
oguro 0:8e42db611280 26 DigitalOut myled(LED1);
oguro 0:8e42db611280 27
oguro 0:8e42db611280 28 float Vr_adc=0.0f;
oguro 0:8e42db611280 29
oguro 0:8e42db611280 30 Timer uT;
oguro 0:8e42db611280 31 float ut1=0,ut2=0,usi=0;
oguro 0:8e42db611280 32 float Speed=0;
oguro 0:8e42db611280 33
oguro 0:8e42db611280 34 void HAH(){
oguro 0:8e42db611280 35
oguro 0:8e42db611280 36 s=r%2;
oguro 0:8e42db611280 37 if(s==0){
oguro 0:8e42db611280 38 ut1=uT.read_us();
oguro 0:8e42db611280 39 r++;
oguro 0:8e42db611280 40 }
oguro 0:8e42db611280 41
oguro 0:8e42db611280 42 if(s==1){
oguro 0:8e42db611280 43 ut2=uT.read_us();
oguro 0:8e42db611280 44 r++;
oguro 0:8e42db611280 45 uT.reset();
oguro 0:8e42db611280 46 }
oguro 0:8e42db611280 47 mypwmA.write(Vr_adc);
oguro 0:8e42db611280 48 mypwmB.write(0);
oguro 0:8e42db611280 49 mypwmC.write(0);
oguro 0:8e42db611280 50 }
oguro 0:8e42db611280 51
oguro 0:8e42db611280 52 void HAL(){
oguro 0:8e42db611280 53
oguro 0:8e42db611280 54 mypwmA.write(0);
oguro 0:8e42db611280 55 mypwmC.write(0);
oguro 0:8e42db611280 56
oguro 0:8e42db611280 57 }
oguro 0:8e42db611280 58 void HBH(){
oguro 0:8e42db611280 59
oguro 0:8e42db611280 60 mypwmA.write(0);
oguro 0:8e42db611280 61 mypwmB.write(Vr_adc);
oguro 0:8e42db611280 62 mypwmC.write(0);
oguro 0:8e42db611280 63 }
oguro 0:8e42db611280 64 void HBL(){
oguro 0:8e42db611280 65
oguro 0:8e42db611280 66 mypwmA.write(0);
oguro 0:8e42db611280 67 mypwmB.write(0);
oguro 0:8e42db611280 68 }
oguro 0:8e42db611280 69 void HCH(){
oguro 0:8e42db611280 70
oguro 0:8e42db611280 71 mypwmA.write(0);
oguro 0:8e42db611280 72 mypwmB.write(0);
oguro 0:8e42db611280 73 mypwmC.write(Vr_adc);
oguro 0:8e42db611280 74 }
oguro 0:8e42db611280 75
oguro 0:8e42db611280 76 void HCL(){
oguro 0:8e42db611280 77 mypwmB.write(0);
oguro 0:8e42db611280 78 mypwmC.write(0);
oguro 0:8e42db611280 79 }
oguro 0:8e42db611280 80
oguro 0:8e42db611280 81 int main() {
oguro 0:8e42db611280 82 pc.baud(128000);
oguro 0:8e42db611280 83
oguro 0:8e42db611280 84 EN1=1;
oguro 0:8e42db611280 85 EN2=1;
oguro 0:8e42db611280 86 EN3=1;
oguro 0:8e42db611280 87
oguro 0:8e42db611280 88 mypwmA.period_us(20); // PWM 50KHz
oguro 0:8e42db611280 89
oguro 0:8e42db611280 90 mypwmB.period_us(20);
oguro 0:8e42db611280 91
oguro 0:8e42db611280 92 mypwmC.period_us(20);
oguro 0:8e42db611280 93
oguro 0:8e42db611280 94
oguro 0:8e42db611280 95 while(1) {
oguro 0:8e42db611280 96
oguro 0:8e42db611280 97 Vr_adc=V_adc.read();
oguro 0:8e42db611280 98 uT.start();
oguro 0:8e42db611280 99
oguro 0:8e42db611280 100 if((Vr_adc>0.15f)&&(q==0)){
oguro 0:8e42db611280 101 while(q<50){
oguro 0:8e42db611280 102
oguro 0:8e42db611280 103 mypwmA.write(0);
oguro 0:8e42db611280 104 mypwmB.write(0.5f);
oguro 0:8e42db611280 105 mypwmC.write(0);
oguro 0:8e42db611280 106 wait_ms(START);
oguro 0:8e42db611280 107
oguro 0:8e42db611280 108 mypwmA.write(0.5f);
oguro 0:8e42db611280 109 mypwmB.write(0);
oguro 0:8e42db611280 110 mypwmC.write(0);
oguro 0:8e42db611280 111 wait_ms(START);
oguro 0:8e42db611280 112
oguro 0:8e42db611280 113 mypwmA.write(0);
oguro 0:8e42db611280 114 mypwmB.write(0);
oguro 0:8e42db611280 115 mypwmC.write(0.5f);
oguro 0:8e42db611280 116 wait_ms(START);
oguro 0:8e42db611280 117 q++;
oguro 0:8e42db611280 118 }
oguro 0:8e42db611280 119 }
oguro 0:8e42db611280 120
oguro 0:8e42db611280 121 HA.rise(&HCH); //HAH
oguro 0:8e42db611280 122 HC.fall(&HBL); //HCL
oguro 0:8e42db611280 123 HB.rise(&HAH); //HBH
oguro 0:8e42db611280 124 HA.fall(&HCL); //HAL
oguro 0:8e42db611280 125 HC.rise(&HBH); //HCH
oguro 0:8e42db611280 126 HB.fall(&HAL); //HBL
oguro 0:8e42db611280 127 // s=0;
oguro 0:8e42db611280 128 if(Vr_adc < 0.1f){
oguro 0:8e42db611280 129
oguro 0:8e42db611280 130 q=0;
oguro 0:8e42db611280 131
oguro 0:8e42db611280 132 }
oguro 0:8e42db611280 133
oguro 0:8e42db611280 134 usi=abs(ut2-ut1);
oguro 0:8e42db611280 135 Speed=60*(1/(7.0*usi*1E-6));
oguro 0:8e42db611280 136 pc.printf("%.3f , %.3f \r" ,Speed ,Vr_adc);
oguro 0:8e42db611280 137 //UP=HA; VP=HB; WP=HC;
oguro 0:8e42db611280 138 // pc.printf("%d ,%d ,%d\r" ,UP,VP,WP);
oguro 0:8e42db611280 139 myled = !myled;
oguro 0:8e42db611280 140
oguro 0:8e42db611280 141 }
oguro 0:8e42db611280 142 }