Forked version by ATalkingDog
Dependencies: mbed existing_references
main.cpp@2:3e2b01ddcc0e, 2021-05-24 (annotated)
- Committer:
- atalkingdog
- Date:
- Mon May 24 09:10:50 2021 +0000
- Revision:
- 2:3e2b01ddcc0e
- Parent:
- 1:26da8b1fd7f4
test;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
helenh | 0:d8cd915552c0 | 1 | #include "mbed.h" |
helenh | 0:d8cd915552c0 | 2 | #include "Serial_Transport.h" |
helenh | 1:26da8b1fd7f4 | 3 | #include "PID_Controller.h" |
helenh | 1:26da8b1fd7f4 | 4 | #include "MOTOR_CONTROL_CLASS.h" |
helenh | 1:26da8b1fd7f4 | 5 | #define pid_p 100 |
helenh | 1:26da8b1fd7f4 | 6 | #define pid_i 10 |
helenh | 1:26da8b1fd7f4 | 7 | #define pid_d 1 |
helenh | 1:26da8b1fd7f4 | 8 | #define pid_k 10 |
helenh | 1:26da8b1fd7f4 | 9 | #define PWM_PERIOD 0.01f //10ms ,100Hz |
helenh | 1:26da8b1fd7f4 | 10 | #define PWM_WIDTH_RATIO 0.00f |
helenh | 0:d8cd915552c0 | 11 | |
helenh | 0:d8cd915552c0 | 12 | DigitalOut myled(LED1); |
helenh | 1:26da8b1fd7f4 | 13 | //Serial pc(SERIAL_TX, SERIAL_RX); |
helenh | 1:26da8b1fd7f4 | 14 | unsigned char recv_buff[100]; |
helenh | 1:26da8b1fd7f4 | 15 | Serial pc(SERIAL_TX, SERIAL_RX); |
helenh | 0:d8cd915552c0 | 16 | |
helenh | 0:d8cd915552c0 | 17 | int main() { |
helenh | 1:26da8b1fd7f4 | 18 | |
helenh | 1:26da8b1fd7f4 | 19 | pc.baud(115200); |
helenh | 1:26da8b1fd7f4 | 20 | |
helenh | 1:26da8b1fd7f4 | 21 | //是否有新的预设置位置 |
helenh | 1:26da8b1fd7f4 | 22 | unsigned char hasrecv_position; |
helenh | 1:26da8b1fd7f4 | 23 | //预设置位置值 |
helenh | 1:26da8b1fd7f4 | 24 | unsigned int preset_value=0; |
helenh | 1:26da8b1fd7f4 | 25 | //实际位置值 |
helenh | 1:26da8b1fd7f4 | 26 | unsigned int actual_value=0; |
helenh | 1:26da8b1fd7f4 | 27 | |
helenh | 1:26da8b1fd7f4 | 28 | //PID输出值 |
helenh | 1:26da8b1fd7f4 | 29 | int controll_value=0; |
helenh | 1:26da8b1fd7f4 | 30 | |
helenh | 1:26da8b1fd7f4 | 31 | //串口屏通讯对象指针 |
helenh | 1:26da8b1fd7f4 | 32 | Serial_class * preset_Com; |
helenh | 1:26da8b1fd7f4 | 33 | //创建实例 |
helenh | 1:26da8b1fd7f4 | 34 | preset_Com=new Serial_class(D1,D0); |
helenh | 1:26da8b1fd7f4 | 35 | |
helenh | 1:26da8b1fd7f4 | 36 | //摄像头通讯对象 |
helenh | 1:26da8b1fd7f4 | 37 | Serial_class * pOpenmv_Com; |
helenh | 1:26da8b1fd7f4 | 38 | //创建实例 |
helenh | 1:26da8b1fd7f4 | 39 | pOpenmv_Com=new Serial_class(D5,D4); |
helenh | 1:26da8b1fd7f4 | 40 | |
helenh | 1:26da8b1fd7f4 | 41 | //声明并创建PID对象 |
helenh | 1:26da8b1fd7f4 | 42 | Ball_PID * ball_pid=new Ball_PID(pid_p,pid_i,pid_d,pid_k); |
helenh | 1:26da8b1fd7f4 | 43 | |
helenh | 1:26da8b1fd7f4 | 44 | //创建PWM对象 |
helenh | 1:26da8b1fd7f4 | 45 | Motor_Control_Class mypwmout(D10); |
helenh | 1:26da8b1fd7f4 | 46 | mypwmout.Set_Para(PWM_PERIOD,PWM_WIDTH_RATIO);//10ms ,100Hz |
helenh | 1:26da8b1fd7f4 | 47 | |
helenh | 0:d8cd915552c0 | 48 | while(1) { |
helenh | 0:d8cd915552c0 | 49 | // myled = 1; |
helenh | 0:d8cd915552c0 | 50 | // wait(0.2); |
helenh | 0:d8cd915552c0 | 51 | // myled = 0; |
helenh | 0:d8cd915552c0 | 52 | // wait(0.2); |
helenh | 0:d8cd915552c0 | 53 | |
helenh | 1:26da8b1fd7f4 | 54 | //pc.printf("data is received succesfully"); |
helenh | 1:26da8b1fd7f4 | 55 | //读取串口屏对象接收的数据 |
helenh | 1:26da8b1fd7f4 | 56 | hasrecv_position=preset_Com->HasRecv(); |
helenh | 1:26da8b1fd7f4 | 57 | pc.printf("state=%d\n",hasrecv_position); |
helenh | 1:26da8b1fd7f4 | 58 | if(hasrecv_position==4) |
helenh | 1:26da8b1fd7f4 | 59 | { |
helenh | 1:26da8b1fd7f4 | 60 | //收到完整的信息,位置值 |
helenh | 1:26da8b1fd7f4 | 61 | preset_value=preset_Com->ReadInfor(); |
helenh | 1:26da8b1fd7f4 | 62 | pc.printf("value=%d",preset_value); |
helenh | 1:26da8b1fd7f4 | 63 | //pc.printf("data is received succesfully"); |
helenh | 1:26da8b1fd7f4 | 64 | } |
helenh | 1:26da8b1fd7f4 | 65 | // |
helenh | 1:26da8b1fd7f4 | 66 | // // |
helenh | 1:26da8b1fd7f4 | 67 | // //读取OPENMV对象接收的数据 |
helenh | 1:26da8b1fd7f4 | 68 | // hasrecv_position=pOpenmv_Com->HasRecv(); |
helenh | 1:26da8b1fd7f4 | 69 | // if(hasrecv_position==1) |
helenh | 1:26da8b1fd7f4 | 70 | // { |
helenh | 1:26da8b1fd7f4 | 71 | // //收到完整的信息,位置值 |
helenh | 1:26da8b1fd7f4 | 72 | // actual_value=pOpenmv_Com->ReadInfor(); |
helenh | 1:26da8b1fd7f4 | 73 | // |
helenh | 1:26da8b1fd7f4 | 74 | // } |
helenh | 1:26da8b1fd7f4 | 75 | // |
helenh | 1:26da8b1fd7f4 | 76 | // //交给PID模块处理,获得控制量 |
helenh | 1:26da8b1fd7f4 | 77 | // controll_value=ball_pid->PID_Calculation(preset_value,actual_value/2); |
helenh | 1:26da8b1fd7f4 | 78 | // |
helenh | 1:26da8b1fd7f4 | 79 | // //转换为占空比并输出 |
helenh | 1:26da8b1fd7f4 | 80 | // mypwmout.Set_Output(controll_value); |
helenh | 1:26da8b1fd7f4 | 81 | // // |
helenh | 1:26da8b1fd7f4 | 82 | wait(0.5); |
atalkingdog | 2:3e2b01ddcc0e | 83 | //lll |
helenh | 0:d8cd915552c0 | 84 | } |
helenh | 0:d8cd915552c0 | 85 | } |
helenh | 0:d8cd915552c0 | 86 | |
helenh | 0:d8cd915552c0 | 87 |