1

Dependencies:   mbed-dev_spine

Committer:
WXD
Date:
Tue Oct 08 04:35:23 2019 +0000
Revision:
5:6a95726e45b0
Child:
6:aad89fd109c2
qqq

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WXD 5:6a95726e45b0 1 #include "data_command.h"
WXD 5:6a95726e45b0 2
WXD 5:6a95726e45b0 3
WXD 5:6a95726e45b0 4 Serial command(PC_10, PC_11);
WXD 5:6a95726e45b0 5 DigitalOut sf_m_c(PC_12);
WXD 5:6a95726e45b0 6
WXD 5:6a95726e45b0 7 int enabled = 0; // 进入电机模式标志位
WXD 5:6a95726e45b0 8 int counter = 0; // 输出计数器
WXD 5:6a95726e45b0 9 int c_control = 0; // 命令帧标志位
WXD 5:6a95726e45b0 10 float SP_pf = 0, SP_df = 0;
WXD 5:6a95726e45b0 11 int c_lock = 2; // 位置锁定标志位
WXD 5:6a95726e45b0 12
WXD 5:6a95726e45b0 13 unsigned int flag_A = 0, flag_B = 0;
WXD 5:6a95726e45b0 14 unsigned int Snum = 0;
WXD 5:6a95726e45b0 15 uint16_t Sget[10] = {0};
WXD 5:6a95726e45b0 16 uint16_t Suse[10] = {0};
WXD 5:6a95726e45b0 17
WXD 5:6a95726e45b0 18
WXD 5:6a95726e45b0 19 void serial_command_isr()
WXD 5:6a95726e45b0 20 {
WXD 5:6a95726e45b0 21 while(command.readable())
WXD 5:6a95726e45b0 22 {
WXD 5:6a95726e45b0 23 uint8_t c = command.getc();
WXD 5:6a95726e45b0 24 if(c == 'A')
WXD 5:6a95726e45b0 25 {
WXD 5:6a95726e45b0 26 flag_A = 1;
WXD 5:6a95726e45b0 27
WXD 5:6a95726e45b0 28 flag_B = 0;
WXD 5:6a95726e45b0 29 Snum = 0;
WXD 5:6a95726e45b0 30 for(unsigned int i = 0; i < 10; i++)
WXD 5:6a95726e45b0 31 {
WXD 5:6a95726e45b0 32 Sget[i] = 0;
WXD 5:6a95726e45b0 33 }
WXD 5:6a95726e45b0 34
WXD 5:6a95726e45b0 35 break;
WXD 5:6a95726e45b0 36 }
WXD 5:6a95726e45b0 37 if(c == 'B')
WXD 5:6a95726e45b0 38 {
WXD 5:6a95726e45b0 39 flag_B = 1;
WXD 5:6a95726e45b0 40 }
WXD 5:6a95726e45b0 41
WXD 5:6a95726e45b0 42 if(flag_A == 1)
WXD 5:6a95726e45b0 43 {
WXD 5:6a95726e45b0 44 if((flag_B != 1) && (Snum < 10))
WXD 5:6a95726e45b0 45 {
WXD 5:6a95726e45b0 46 Sget[Snum] = c;
WXD 5:6a95726e45b0 47 }
WXD 5:6a95726e45b0 48
WXD 5:6a95726e45b0 49 Snum++;
WXD 5:6a95726e45b0 50
WXD 5:6a95726e45b0 51 if((flag_B == 1) && (Snum != 11))
WXD 5:6a95726e45b0 52 {
WXD 5:6a95726e45b0 53 flag_A = 0;
WXD 5:6a95726e45b0 54 flag_B = 0;
WXD 5:6a95726e45b0 55 Snum = 0;
WXD 5:6a95726e45b0 56 }
WXD 5:6a95726e45b0 57
WXD 5:6a95726e45b0 58 if((flag_B == 1) && (Snum == 11))
WXD 5:6a95726e45b0 59 {
WXD 5:6a95726e45b0 60 flag_A = 0;
WXD 5:6a95726e45b0 61 flag_B = 0;
WXD 5:6a95726e45b0 62 Snum = 0;
WXD 5:6a95726e45b0 63
WXD 5:6a95726e45b0 64 for(unsigned int i = 0; i < 10; i++)
WXD 5:6a95726e45b0 65 {
WXD 5:6a95726e45b0 66 Suse[i] = Sget[i];
WXD 5:6a95726e45b0 67 }
WXD 5:6a95726e45b0 68
WXD 5:6a95726e45b0 69 c_control = 1; // 命令帧接收成功
WXD 5:6a95726e45b0 70
WXD 5:6a95726e45b0 71 //pc.printf("%c%c%c%c%c%c%c%c%c%c\n\r", S340use[0], S340use[1], S340use[2], S340use[3], S340use[4], S340use[5], S340use[6], S340use[7], S340use[8], S340use[9]);
WXD 5:6a95726e45b0 72 }
WXD 5:6a95726e45b0 73 }
WXD 5:6a95726e45b0 74 }
WXD 5:6a95726e45b0 75 }
WXD 5:6a95726e45b0 76
WXD 5:6a95726e45b0 77
WXD 5:6a95726e45b0 78 void command_control()
WXD 5:6a95726e45b0 79 {
WXD 5:6a95726e45b0 80 SP_pf = 10*(Suse[2] - '0') + 1*(Suse[3] - '0') + 0.1*(Suse[4] - '0');
WXD 5:6a95726e45b0 81 SP_df = 10*(Suse[7] - '0') + 1*(Suse[8] - '0') + 0.1*(Suse[9] - '0');
WXD 5:6a95726e45b0 82 if(Suse[1] == '+')
WXD 5:6a95726e45b0 83 SP_pf = 0 + SP_pf;
WXD 5:6a95726e45b0 84 if(Suse[1] == '-')
WXD 5:6a95726e45b0 85 SP_pf = 0 - SP_pf;
WXD 5:6a95726e45b0 86 if(Suse[6] == '+')
WXD 5:6a95726e45b0 87 SP_df = 0 + SP_df;
WXD 5:6a95726e45b0 88 if(Suse[6] == '-')
WXD 5:6a95726e45b0 89 SP_df = 0 - SP_df;
WXD 5:6a95726e45b0 90
WXD 5:6a95726e45b0 91
WXD 5:6a95726e45b0 92 sf_m_c = 1; // 切换为发送模式
WXD 5:6a95726e45b0 93 wait_us(200);
WXD 5:6a95726e45b0 94
WXD 5:6a95726e45b0 95 switch(Suse[0])
WXD 5:6a95726e45b0 96 {
WXD 5:6a95726e45b0 97 case('e'):
WXD 5:6a95726e45b0 98 command.printf("\n\rPF exiting motor mode\r");
WXD 5:6a95726e45b0 99 ExitMotorMode(&PF_can);
WXD 5:6a95726e45b0 100 c_lock = 1; // 电机位置解锁
WXD 5:6a95726e45b0 101 send_enable = 0; // main不发送CAN位置命令
WXD 5:6a95726e45b0 102 break;
WXD 5:6a95726e45b0 103 case('m'):
WXD 5:6a95726e45b0 104 command.printf("\n\rPF entering PC motor mode\r");
WXD 5:6a95726e45b0 105 EnterMotorMode(&PF_can);
WXD 5:6a95726e45b0 106 c_lock = 1; // 电机位置锁定
WXD 5:6a95726e45b0 107 send_enable = 1; // main发送CAN位置命令
WXD 5:6a95726e45b0 108 break;
WXD 5:6a95726e45b0 109 case('M'):
WXD 5:6a95726e45b0 110 command.printf("\n\rPF entering BOARD motor mode\r");
WXD 5:6a95726e45b0 111 EnterMotorMode(&PF_can);
WXD 5:6a95726e45b0 112 c_lock = 0; // 电机位置解锁
WXD 5:6a95726e45b0 113 send_enable = 0; // M模式下,命令是否发送,命令是什么由calculate决定
WXD 5:6a95726e45b0 114 break;
WXD 5:6a95726e45b0 115 case('z'):
WXD 5:6a95726e45b0 116 command.printf("\n\rPF zeroing\r");
WXD 5:6a95726e45b0 117 Zero(&PF_can);
WXD 5:6a95726e45b0 118 break;
WXD 5:6a95726e45b0 119 }
WXD 5:6a95726e45b0 120 switch(Suse[5])
WXD 5:6a95726e45b0 121 {
WXD 5:6a95726e45b0 122 case('e'):
WXD 5:6a95726e45b0 123 command.printf("DF exiting motor mode\n\r");
WXD 5:6a95726e45b0 124 ExitMotorMode(&DF_can);
WXD 5:6a95726e45b0 125 c_lock = 1; // 电机位置解锁
WXD 5:6a95726e45b0 126 send_enable = 0; // main不发送CAN位置命令
WXD 5:6a95726e45b0 127 break;
WXD 5:6a95726e45b0 128 case('m'):
WXD 5:6a95726e45b0 129 command.printf("DF entering PC motor mode\n\r");
WXD 5:6a95726e45b0 130 EnterMotorMode(&DF_can);
WXD 5:6a95726e45b0 131 c_lock = 1; // 电机位置锁定
WXD 5:6a95726e45b0 132 send_enable = 1; // main发送CAN位置命令
WXD 5:6a95726e45b0 133 break;
WXD 5:6a95726e45b0 134 case('M'):
WXD 5:6a95726e45b0 135 command.printf("DF entering BOARD motor mode\n\r");
WXD 5:6a95726e45b0 136 EnterMotorMode(&DF_can);
WXD 5:6a95726e45b0 137 c_lock = 0; // 电机位置解锁
WXD 5:6a95726e45b0 138 send_enable = 0; // M模式下,命令是否发送,命令是什么由calculate决定
WXD 5:6a95726e45b0 139 break;
WXD 5:6a95726e45b0 140 case('z'):
WXD 5:6a95726e45b0 141 command.printf("DF zeroing\n\r");
WXD 5:6a95726e45b0 142 Zero(&DF_can);
WXD 5:6a95726e45b0 143 break;
WXD 5:6a95726e45b0 144 }
WXD 5:6a95726e45b0 145
WXD 5:6a95726e45b0 146 wait_ms(2);
WXD 5:6a95726e45b0 147 sf_m_c = 0; // 恢复接收模式
WXD 5:6a95726e45b0 148 wait_us(200);
WXD 5:6a95726e45b0 149
WXD 5:6a95726e45b0 150
WXD 5:6a95726e45b0 151 // 电机模式命令在此发送,锁定控制模式的位置量在main中发送
WXD 5:6a95726e45b0 152 WriteAll();
WXD 5:6a95726e45b0 153 wait(1.0f);
WXD 5:6a95726e45b0 154
WXD 5:6a95726e45b0 155 // 解除串口控制,缓冲区清0
WXD 5:6a95726e45b0 156 c_control = 0;
WXD 5:6a95726e45b0 157 for(unsigned int i = 0; i < 10; i++)
WXD 5:6a95726e45b0 158 {
WXD 5:6a95726e45b0 159 Suse[i] = 0;
WXD 5:6a95726e45b0 160 }
WXD 5:6a95726e45b0 161 }
WXD 5:6a95726e45b0 162
WXD 5:6a95726e45b0 163
WXD 5:6a95726e45b0 164
WXD 5:6a95726e45b0 165
WXD 5:6a95726e45b0 166
WXD 5:6a95726e45b0 167
WXD 5:6a95726e45b0 168
WXD 5:6a95726e45b0 169
WXD 5:6a95726e45b0 170
WXD 5:6a95726e45b0 171
WXD 5:6a95726e45b0 172
WXD 5:6a95726e45b0 173
WXD 5:6a95726e45b0 174
WXD 5:6a95726e45b0 175
WXD 5:6a95726e45b0 176
WXD 5:6a95726e45b0 177
WXD 5:6a95726e45b0 178
WXD 5:6a95726e45b0 179
WXD 5:6a95726e45b0 180
WXD 5:6a95726e45b0 181
WXD 5:6a95726e45b0 182
WXD 5:6a95726e45b0 183
WXD 5:6a95726e45b0 184
WXD 5:6a95726e45b0 185
WXD 5:6a95726e45b0 186
WXD 5:6a95726e45b0 187
WXD 5:6a95726e45b0 188
WXD 5:6a95726e45b0 189
WXD 5:6a95726e45b0 190
WXD 5:6a95726e45b0 191
WXD 5:6a95726e45b0 192
WXD 5:6a95726e45b0 193
WXD 5:6a95726e45b0 194
WXD 5:6a95726e45b0 195
WXD 5:6a95726e45b0 196
WXD 5:6a95726e45b0 197
WXD 5:6a95726e45b0 198
WXD 5:6a95726e45b0 199
WXD 5:6a95726e45b0 200
WXD 5:6a95726e45b0 201
WXD 5:6a95726e45b0 202