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
Fork of Robotics_LAB_motor by
main.cpp
- Committer:
- ryan820909
- Date:
- 2017-03-21
- Revision:
- 1:13ce5b28f6dd
- Parent:
- 0:74ea99c4db88
File content as of revision 1:13ce5b28f6dd:
#include "mbed.h"
Serial pc(USBTX,USBRX);
//The number will be compiled as type "double" in default
//Add a "f" after the number can make it compiled as type "float"
#define Ts 0.01f //period of timer1 (s)
Ticker timer1;
// servo motor
PwmOut servo_cmd(A0);
// DC motor
PwmOut pwm1(D7);
PwmOut pwm1n(D11);
PwmOut pwm2(D8);
PwmOut pwm2n(A3);
// Motor1 sensor
InterruptIn HallA(A1);
InterruptIn HallB(A2);
// Motor2 sensor
InterruptIn HallA_2(D13);
InterruptIn HallB_2(D12);
// 函式宣告
void init_IO();
void init_TIMER();
void timer1_ITR();
void init_CN_1();
void init_CN_2();
void CN_ITR_1();
void CN_ITR_2();
void init_PWM();
// servo motor
float servo_duty = 0.025; // 0.069 +(0.088/180)*angle, -90<angle<90
// 90度->duty=0.025; 0度->duty=0.069; -90度->duty=0.113
int angle = 0;
// Hall sensor
int HallA_1_state = 0;
int HallB_1_state = 0;
int state_1 = 0;
int state_1_old = 0;
int HallA_2_state = 0;
int HallB_2_state = 0;
int state_2 = 0;
int state_2_old = 0;
// DC motor rotation speed control
int speed_count_1 = 0;
float rotation_speed_1 = 0.0;
float rotation_speed_ref_1 = 0;
float pwm1_duty = 0.5;
float PI_out_1 = 0.0;
float err_1 = 0.0;
float ierr_1 = 0.0;
int speed_count_2 = 0;
float rotation_speed_2 = 0.0;
float rotation_speed_ref_2 = 150;
float pwm2_duty = 0.5;
float PI_out_2 = 0.0;
float err_2 = 0.0;
float ierr_2 = 0.0;
float Kp = 0.4;
float Ki = 0.1;
int main()
{
init_PWM();
init_CN_1();
init_CN_2();
init_TIMER();
while(1) {
}
}
void init_TIMER()
{
timer1.attach_us(&timer1_ITR, 10000.0); // the address of the function to be attached (timer1_ITR) and the interval (10000 micro-seconds)
}
void init_PWM()
{
servo_cmd.period_ms(20);
servo_cmd.write(servo_duty);
pwm1.period_us(50);
pwm1.write(0.5f);
TIM1->CCER |= 0x4;
pwm2.period_us(50);
pwm2.write(0.5f);
TIM1->CCER |= 0x40;
}
void init_CN_1()
{
HallA.rise(&CN_ITR_1);
HallA.fall(&CN_ITR_1);
HallB.rise(&CN_ITR_1);
HallB.fall(&CN_ITR_1);
}
void init_CN_2()
{
HallA_2.rise(&CN_ITR_2);
HallA_2.fall(&CN_ITR_2);
HallB_2.rise(&CN_ITR_2);
HallB_2.fall(&CN_ITR_2);
}
void CN_ITR_1()
{
speed_count_1 = speed_count_1+1;
}
void CN_ITR_2()
{
speed_count_2 = speed_count_2+1;
}
void timer1_ITR()
{
// servo motor
///code for sevo motor///
/////////////////////////
if(servo_duty >= 0.113f)servo_duty = 0.113;
else if(servo_duty <= 0.025f)servo_duty = 0.025;
servo_cmd.write(servo_duty);
// motor1
rotation_speed_1 = (float)speed_count_1 * 100.0f / 12.0f * 60.0f / 29.0f; //unit: rpm
// pc.printf("%f ",rotation_speed_1);
speed_count_1 = 0;
///PI controller for motor1///
//////////////////////////////
/*
if(PI_out_1 >= 0.5f)PI_out_1 = 0.5;
else if(PI_out_1 <= -0.5f)PI_out_1 = -0.5;
pwm1_duty = PI_out_1 + 0.5f;
pwm1.write(pwm1_duty);
TIM1->CCER |= 0x4;
*/
//motor2
// rotation_speed_2 = (float)speed_count_2 * 100.0f / 12.0f * 60.0f / 29.0f; //unit: rpm
pc.printf("%d \n\r",speed_count_2*100/12*60/29);
speed_count_2 = 0;
///PI controller for motor2///
err_2 = Kp*(rotation_speed_ref_2-rotation_speed_2);
ierr_2 = ierr_2 +Ki*(rotation_speed_ref_2-rotation_speed_2)*0.01;
PI_out_2 = (float)(err_2)*0.005;
//////////////////////////////
if(PI_out_2 >= 0.5f)PI_out_2 = 0.5;
else if(PI_out_2 <= -0.5f)PI_out_2 = -0.5;
pwm2_duty = PI_out_2 + 0.5f;
pwm2.write(pwm2_duty);
TIM1->CCER |= 0x40;
}
