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.
Dependencies: mbed QEI mbed-rtos
main.cpp
- Committer:
- kosaka
- Date:
- 2013-01-04
- Revision:
- 12:459af534d1ee
- Parent:
- 11:9747752435d1
- Child:
- 13:ba71733c11d7
File content as of revision 12:459af534d1ee:
// DC motor control program using H-bridge driver (ex. TA7291P) and 360 resolution rotary encoder with A, B phase.
// ver. 121224 by Kosaka lab.
#include "mbed.h"
#include "rtos.h"
#include "controller.h"
#include "Hbridge.h"
Serial pc2(USBTX, USBRX); // Display on tera term in PC
LocalFileSystem local("mbedUSBdrive"); // save data to mbed USB disk drive in PC
Ticker TickerTimerTS0; // Timer interrupt using TIMER3, TS<0.001 is OK. Priority is higher than rtosTimer.
unsigned char fTimerTS2ON=0, fTimerTS3ON=0, fTimerTS4ON=0; // ON/OFF flag for timerTS2, 3, 4.
//DigitalOut debug_p24(p24); // p17 for debug
extern "C" void mbed_reset();
void CallTimerTS2(void const *argument) { // make sampling time TS3 timer (priority 3: precision 4ms)
int ms;
unsigned long c;
while (true) {
c = _count;
if( fTimerTS2ON ){
timerTS2(); // timerTS2() is called every TS2[s].
}
if( (ms=(int)(TS2*1000-(_count-c)*TS0*1000))<=0 ){ ms=1;}
Thread::wait(ms);
}
}
void CallTimerTS3(void const *argument) { // make sampling time TS3 timer (priority 3: precision 4ms)
int ms;
unsigned long c;
while (true) {
c = _count;
if( fTimerTS3ON ){
timerTS3(); // timerTS3() is called every TS3[s].
}
if( (ms=(int)(TS3*1000-(_count-c)*TS0*1000))<=0 ){ ms=1;}
Thread::wait(ms);
}
}
void CallTimerTS4(void const *argument) { // make sampling time TS4 timer (priority 4: precision 4ms)
int ms;
unsigned long c;
while (true) {
c = _count;
if( fTimerTS4ON ){
timerTS4(); // timerTS4() is called every TS4[s].
}
if( (ms=(int)(TS4*1000-(_count-c)*TS0*1000))<=0 ){ ms=1;}
Thread::wait(ms);
}
}
//#define OLD
int main(){
unsigned short i;
FILE *fp = fopen("/mbedUSBdrive/data.csv", "w"); // save data to PC
RtosTimer RtosTimerTS1(timerTS1); // RtosTimer priority is osPriorityAboveNormal, just one above main()
Thread ThreadTimerTS3(CallTimerTS3,NULL,osPriorityBelowNormal);
Thread ThreadTimerTS4(CallTimerTS4,NULL,osPriorityLow);
// Priority of Thread (RtosTimer is osPriorityAboveNormal)
// osPriorityIdle = -3, ///< priority: idle (lowest)--> then, mbed ERROR!!
// osPriorityLow = -2, ///< priority: low
// osPriorityBelowNormal = -1, ///< priority: below normal
// osPriorityNormal = 0, ///< priority: normal (default)
// osPriorityAboveNormal = +1, ///< priority: above normal
// osPriorityHigh = +2, ///< priority: high
// osPriorityRealtime = +3, ///< priority: realtime (highest)
// osPriorityError = 0x84 ///< system cannot determine priority or thread has illegal priority
// 指令速度
float w_ref_req[2] = {2* 2*PI, 4* 2*PI}; // [rad/s](第2要素は指令速度急変後の指令速度)
float t; // current time
// IPMSMの機器定数等の設定, 制御器の初期化
init_parameters();
// シミュレーション開始
pc2.printf("Simulation start!!\r\n");
#ifndef OLD
// start control (ON)
start_pwm();
TickerTimerTS0.attach(&timerTS0, TS0 ); // Sampling period[s] of i_controller
RtosTimerTS1.start((unsigned int)(TS1*1000.)); // Sampling period[ms] of th controller
fTimerTS3ON = 1; // timerTS3 start
fTimerTS4ON = 1; // timerTS3 start
#endif
while( (t = _count*TS0) < TMAX ){
// debug_p24 = 1;
// 速度急変
if( 0.26<=t && t<2.3 ){
vl.w_ref=w_ref_req[1]; // 目標速度をメインルーチンから速度制御メインループへ渡す。
}else{
vl.w_ref=w_ref_req[0];
}
#ifdef SIMULATION
// 負荷トルク急変
if( t<3.4 ){
p.TL = 1;
}else{
p.TL = 2;
}
#endif
#ifdef OLD
if( (++i2)>=(int)(TS1/TS0) ){ i2=0;
timerTS1(&j); //velocity_loop(); // 速度制御メインループ(w_ref&beta_ref to idq_ref)
}
#endif
#ifdef OLD
timerTS0();
#endif
// debug_p24 = 0;
Thread::wait(1);
}
// stop timers (OFF)
stop_pwm();
TickerTimerTS0.detach(); // timer interrupt stop
RtosTimerTS1.stop(); // rtos timer stop
fTimerTS3ON=0;//ThreadTimerTS3.terminate(); //
fTimerTS4ON=0;//ThreadTimerTS4.terminate(); //
// save data to mbed USB drive
for(i=0;i<_count_data;i++){
fprintf( fp, "%f, %f, %f, %f, %f\r\n",
data[i][0],data[i][1],data[i][2],data[i][3],data[i][4]); // save data to PC (para, y, time, u)
}
fclose( fp ); // release mbed USB drive
Thread::wait(100);
pc2.printf("Control completed!!\r\n\r\n");
}