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
- Committer:
- oguro
- Date:
- 2019-06-08
- Revision:
- 2:0146f9f7701c
- Parent:
- 1:ce18261eee75
File content as of revision 2:0146f9f7701c:
#include "mbed.h"
#include "rtos.h"
//#include "adc.h"
#include <math.h>
#define TS1 0.2
float PI=3.141592;
int t=0,q=0,i=0;
int START=7;
PwmOut mypwmA(PA_8);
PwmOut mypwmB(PA_9);
PwmOut mypwmC(PA_10);
PwmOut Current_Ref(PB_4);
DigitalOut Vector(PC_4);
DigitalOut EN1(PC_10);
DigitalOut EN2(PC_11);
DigitalOut EN3(PC_12);
//AnalogIn V_adc(PB_1); //volume
AnalogIn V_adc(PC_2); // Gaibu Potention
AnalogIn Vshuntu(PA_1);
AnalogIn Vshuntv(PA_0);
AnalogIn Vshuntw(PB_0);
AnalogIn BEMF1(PC_3);//C7_37
AnalogIn BEMF2(PB_0);//C7_34
AnalogIn BEMF3(PA_7);//C10_26
InterruptIn HA(PA_15);
InterruptIn HB(PB_3);
InterruptIn HC(PB_10);
Timer uT;
Timer vT;
Timer wT;
//AnalogIn VBUS(PA_1);
DigitalIn GPIO_BEMF(PC_9);
DigitalIn CPOUT(PA_12);
AnalogIn Curr_ui(PA_0);
AnalogIn Curr_vi(PC_1);
AnalogIn Curr_wi(PA_1);
AnalogOut SWAVE(PA_4);
Serial pc(USBTX,USBRX);
DigitalOut myled(LED1);
float sq32=sqrt(3.0f/2.0f);
float sq23=2.0f/sqrt(3.0f);
float sq3=1.0f/sqrt(3.0f);
float Vdlink=3.0f/5.0f;
float aVa;
float a3Vb;
float Vr_adc;
Timer Timer1;
float freq=0;
float z=0.5;
float Wt,Va,Vb,Vq,Vd;
float d1,d2,d3,d4,d5,d6,d07;
float du,dv,dw;
bool cu,cv,cw;
float ave=0,aveo=0,avsum=0;
float zet=sqrt(2.0f/3.0f),cos23=cos((2.0f/3.0f)*PI);
float cos43=cos((4.0f/3.0f)*PI),sin23=sin((2.0f/3.0f)*PI),sin43=sin((4.0f/3.0f)*PI);
float ut=0,ut1=0,ut2=0,usi=0;
float vt=0,vt1=0,vt2=0,vsi=0;
float wt=0,wt1=0,wt2=0,wsi=0;
float Speed;
void HAH(){
ut1=uT.read_us();
ut=0;
}
void HAL(){
ut2=uT.read_us();
uT.reset();
}
void HBH(){
vt1=vT.read_us();
}
void HBL(){
vt2=vT.read_us();
vT.reset();
}
void HCH(){
wt1=wT.read_us();
}
void HCL(){
wt2=wT.read_us();
wT.reset();
}
void CPLT(){
pc.printf("%.3f , %.3f \r" ,Speed ,Vr_adc);
}
void timerTS1(void const*argument){
CPLT();
}
int main() {
Timer1.start();
EN1=1;
EN2=1;
EN3=1;
uT.start();
vT.start();
wT.start();
mypwmA.period_us(20);
mypwmB.period_us(20);
mypwmC.period_us(20);
pc.baud(128000);
RtosTimer RtosTimerTS1(timerTS1);
RtosTimerTS1.start((unsigned int)(TS1*5000)); //3000
Thread::wait(100);
Vq=0.7;
Vd=0;
while(1) {
HA.rise(&HAH);
HC.fall(&HCL);
HB.rise(&HBH);
HA.fall(&HAL);
HC.rise(&HCH);
HB.fall(&HBL);
Speed=60*(1/(7.0*2.0*usi*1E-6));
usi=ut2-ut1;
vsi=vt2-vt1;
wsi=wt2-wt1;
float s = Timer1.read();
Vr_adc=V_adc.read();
myled = ((int)s) % 2 == 0; /* LED output(0.5Hz) */
if((Vr_adc>0.08f)&&(q==0)){
while(q<50){
mypwmA.write(0.5f);
mypwmB.write(0);
mypwmC.write(0);
wait_ms(START);
mypwmA.write(0);
mypwmB.write(0.5f);
mypwmC.write(0.0);
wait_ms(START);
mypwmA.write(0);
mypwmB.write(0.0);
mypwmC.write(0.5f);
wait_ms(START);
q++;
}
}
if(Vr_adc < 0.05f){
q=0;
Vr_adc=0;
}
aveo=Vr_adc;
if((aveo<=0.15)&&(aveo>0.1)){
Wt=fmodf(s * 50, 1) * PI* 2;
Vq=0.4;
}
if((aveo<=0.2)&&(aveo>0.15)){
Wt=fmodf(s * 55, 1) * PI* 2;
Vq=0.45;
}
if((aveo<=0.25)&&(aveo>0.2)){
Wt=fmodf(s * 60, 1) * PI* 2;
Vq=0.5;
}
if((aveo<=0.3)&&(aveo>0.25)){
Wt=fmodf(s * 65, 1) * PI* 2;
Vq=0.55;
}
if((aveo<=0.35)&&(aveo>0.3)){
Wt=fmodf(s * 70, 1) * PI* 2;
Vq=0.6;
}
if((aveo<=0.4)&&(aveo>0.35)){
Wt=fmodf(s * 80, 1) * PI* 2;
Vq=0.65;
}
if((aveo<=0.45)&&(aveo>0.4)){
Wt=fmodf(s * 90, 1) * PI* 2;
Vq=0.7;
}
if((aveo<=0.5)&&(aveo>0.45)){
Wt=fmodf(s * 100, 1) * PI* 2;
Vq=0.7;
}
if((aveo<=0.55)&&(aveo>0.5)){
Wt=fmodf(s * 110, 1) * PI* 2;
Vq=0.7;
}
if((aveo<=0.6)&&(aveo>0.55)){
Wt=fmodf(s * 120, 1) * PI* 2;
Vq=0.7;
}
if((aveo<=0.65)&&(aveo>0.6)){
Wt=fmodf(s * 130, 1) * PI* 2;
Vq=0.8;
}
if((aveo<=0.7)&&(aveo>0.65)){
Wt=fmodf(s * 140, 1) * PI* 2;
Vq=0.8;
}
if((aveo<=0.75)&&(aveo>0.7)){
Wt=fmodf(s * 150, 1) * PI* 2;
Vq=0.8;
}
if((aveo<=0.8)&&(aveo>0.75)){
Wt=fmodf(s * 160, 1) * PI* 2;
Vq=0.8;
}
if((aveo<=0.85)&&(aveo>0.8)){
Wt=fmodf(s * 170, 1) * PI* 2;
Vq=0.9;
}
if((aveo<=0.9)&&(aveo>0.85)){
Wt=fmodf(s * 180, 1) * PI* 2;
Vq=0.9;
}
if((aveo<=0.95)&&(aveo>0.9)){
Wt=fmodf(s * 190, 1) * PI* 2;
Vq=0.9;
}
if(aveo>0.95){
Wt=fmodf(s * 200, 1) * PI* 2;
Vq=0.9;
}
if(aveo<0.1){
mypwmA.write(0);
mypwmB.write(0);
mypwmC.write(0);
}
else{
Va=cos(Wt)*Vd-sin(Wt)*Vq;
Vb=sin(Wt)*Vd+cos(Wt)*Vq;
du=(Va*zet+0.5);
dv=((Va*cos23+Vb*sin23)*zet+0.5);
dw=((Va*cos43+Vb*sin43)*zet+0.5);
mypwmA.write(du);
mypwmB.write(dv);
mypwmC.write(dw);
SWAVE=dw;
}
}
}