Sub programs of serial_transport, PID and motor contrl.

Dependents:   tracking_ball_0516 tracking_ball_0516

Committer:
helenh
Date:
Sun May 30 01:39:35 2021 +0000
Revision:
7:fd405402ff8c
Parent:
6:76f3f6b641e9
e

Who changed what in which revision?

UserRevisionLine numberNew contents of line
helenh 5:9f066f9abe5d 1 //#include "mbed.h"
helenh 0:7f16c88e4047 2 #include "Serial_Transport.h"
helenh 0:7f16c88e4047 3
helenh 2:a668eb71516b 4 //Serial pc(SERIAL_TX, SERIAL_RX);
helenh 2:a668eb71516b 5
helenh 0:7f16c88e4047 6 //串口通讯子程序
helenh 0:7f16c88e4047 7
helenh 1:09e2d1034894 8 //设置环形数组结构
helenh 1:09e2d1034894 9
helenh 1:09e2d1034894 10
helenh 1:09e2d1034894 11 //设计,
helenh 1:09e2d1034894 12 //写入数据:当接受到数据时,若Write_Index==Read_Index,表示数组已满,不能写入,需等待读出。若不相等,则写入Write_Index开始的数组,每完成一个字符的写入,Write_Index加1后取余为新的Write_Index。若Write_Index==Read_Index,则表示队列已满,溢出。
helenh 1:09e2d1034894 13 //读取数据:从数组的Read_Index开始读取数据,若Read_Index==Tail_Index,表示数组的数据为空(或已读完),停止读出。若不相等,则读一个字符,然后Read_Index加1后取余为新的Read_Index。
helenh 1:09e2d1034894 14
helenh 1:09e2d1034894 15 //相关程序
helenh 1:09e2d1034894 16 //声明环形数组
helenh 1:09e2d1034894 17
helenh 1:09e2d1034894 18 //extern Serial openmv_port(D5, D4);
helenh 1:09e2d1034894 19 //extern Serial pc(SERIAL_TX, SERIAL_RX);
helenh 1:09e2d1034894 20
helenh 0:7f16c88e4047 21 //Circle_Struct circle_Struct;
helenh 1:09e2d1034894 22
helenh 0:7f16c88e4047 23 //unsigned char state=0;
helenh 4:82765e1fd9db 24 //unsigned char count;
helenh 3:57962324a4a0 25
helenh 0:7f16c88e4047 26 //对环形数组的操作
helenh 0:7f16c88e4047 27 //判断是否为空
helenh 2:a668eb71516b 28 unsigned char Serial_class::HasRecv(void)
helenh 0:7f16c88e4047 29 {
helenh 0:7f16c88e4047 30 //
helenh 2:a668eb71516b 31 //return infor_struct.state==(4?1:0);
helenh 2:a668eb71516b 32 return infor_struct.state ;
helenh 0:7f16c88e4047 33 //
helenh 0:7f16c88e4047 34 }
helenh 0:7f16c88e4047 35
helenh 0:7f16c88e4047 36
helenh 0:7f16c88e4047 37 //////////////////////////////////////////////////////////////
helenh 0:7f16c88e4047 38 //数据接收:
helenh 2:a668eb71516b 39 void Serial_class::Add_Char(char recv)
helenh 2:a668eb71516b 40 {
helenh 2:a668eb71516b 41 //
helenh 2:a668eb71516b 42 switch(infor_struct.state)
helenh 2:a668eb71516b 43 {
helenh 2:a668eb71516b 44
helenh 2:a668eb71516b 45 case 0: //判断是否是帧头1
helenh 2:a668eb71516b 46 infor_struct.count=0;
helenh 2:a668eb71516b 47 if(recv==0x99)
helenh 2:a668eb71516b 48 infor_struct.state=1;
helenh 2:a668eb71516b 49 break;
helenh 2:a668eb71516b 50 case 1: //判断是否是帧头2
helenh 2:a668eb71516b 51 if(recv==0x24)
helenh 2:a668eb71516b 52 {
helenh 2:a668eb71516b 53 infor_struct.state=2;
helenh 2:a668eb71516b 54 infor_struct.count=0; //信息部分计数器初始化
helenh 2:a668eb71516b 55 infor_struct.value=0;
helenh 2:a668eb71516b 56 }
helenh 2:a668eb71516b 57 else
helenh 2:a668eb71516b 58 {
helenh 2:a668eb71516b 59 //错误,
helenh 2:a668eb71516b 60 if(recv!=0x99)
helenh 2:a668eb71516b 61 infor_struct.state=0;
helenh 2:a668eb71516b 62 }
helenh 2:a668eb71516b 63 break;
helenh 2:a668eb71516b 64 case 2: //信息部分,存入数组
helenh 2:a668eb71516b 65 //是否是字符串结束符
helenh 2:a668eb71516b 66 if(recv==0x0d)
helenh 2:a668eb71516b 67 {
helenh 2:a668eb71516b 68 //
helenh 2:a668eb71516b 69 infor_struct.state=3;
helenh 2:a668eb71516b 70
helenh 2:a668eb71516b 71 }
helenh 2:a668eb71516b 72 else
helenh 2:a668eb71516b 73 {
helenh 2:a668eb71516b 74 infor_struct.recv_buff[infor_struct.count]=recv;
helenh 2:a668eb71516b 75 infor_struct.value*=10;
helenh 3:57962324a4a0 76 infor_struct.value+=(recv-0x30);
helenh 2:a668eb71516b 77 infor_struct.count++;
helenh 2:a668eb71516b 78 }
helenh 2:a668eb71516b 79 break;
helenh 2:a668eb71516b 80 case 3: //wait for 0x0a
helenh 2:a668eb71516b 81 if(recv==0x0a)
helenh 2:a668eb71516b 82 {
helenh 2:a668eb71516b 83 infor_struct.state=4;
helenh 2:a668eb71516b 84 }
helenh 2:a668eb71516b 85 else
helenh 2:a668eb71516b 86 infor_struct.state=0;
helenh 2:a668eb71516b 87 break;
helenh 2:a668eb71516b 88 }
helenh 2:a668eb71516b 89 }
helenh 0:7f16c88e4047 90
helenh 0:7f16c88e4047 91
helenh 0:7f16c88e4047 92 //采用中断方式接收串口信息,设置中断处理函数
helenh 1:09e2d1034894 93 void Serial_class::Do_Recv()
helenh 0:7f16c88e4047 94 {
helenh 3:57962324a4a0 95
helenh 0:7f16c88e4047 96 //临时存储接收的字符串
helenh 6:76f3f6b641e9 97 char buff[1];
helenh 5:9f066f9abe5d 98 //char tem;
helenh 5:9f066f9abe5d 99 int size,size_p;
helenh 5:9f066f9abe5d 100 unsigned char i;
helenh 5:9f066f9abe5d 101
helenh 5:9f066f9abe5d 102 //调试用
helenh 5:9f066f9abe5d 103
helenh 5:9f066f9abe5d 104 if(this->s_port->readable())
helenh 5:9f066f9abe5d 105 {
helenh 5:9f066f9abe5d 106 //
helenh 5:9f066f9abe5d 107 size=this->s_port->read(buff,1);
helenh 5:9f066f9abe5d 108 this->s_port->write(buff,1);
helenh 5:9f066f9abe5d 109
helenh 5:9f066f9abe5d 110 //rw_mutex.lock();
helenh 5:9f066f9abe5d 111
helenh 5:9f066f9abe5d 112 //for(i=0;i<size;i++)
helenh 5:9f066f9abe5d 113 {
helenh 5:9f066f9abe5d 114 //解析加入存储
helenh 5:9f066f9abe5d 115 this->Add_Char(buff[0]);
helenh 5:9f066f9abe5d 116 //
helenh 5:9f066f9abe5d 117 }
helenh 5:9f066f9abe5d 118 //rw_mutex.unlock();
helenh 5:9f066f9abe5d 119 //*/
helenh 5:9f066f9abe5d 120 }
helenh 5:9f066f9abe5d 121
helenh 5:9f066f9abe5d 122 // *pled = !(*pled);
helenh 5:9f066f9abe5d 123
helenh 5:9f066f9abe5d 124
helenh 5:9f066f9abe5d 125
helenh 5:9f066f9abe5d 126 //size=sprintf(this->printbuff,"thread terminal\n");
helenh 5:9f066f9abe5d 127 //printtouart(this->printbuff,size);
helenh 5:9f066f9abe5d 128
helenh 5:9f066f9abe5d 129 }
helenh 5:9f066f9abe5d 130
helenh 5:9f066f9abe5d 131 //
helenh 5:9f066f9abe5d 132 void Serial_class::Do_Recv_E(UnbufferedSerial * pserial)
helenh 5:9f066f9abe5d 133 {
helenh 5:9f066f9abe5d 134
helenh 5:9f066f9abe5d 135 //临时存储接收的字符串
helenh 5:9f066f9abe5d 136 char buff[50];
helenh 5:9f066f9abe5d 137 //char tem;
helenh 5:9f066f9abe5d 138 int size,size_p;
helenh 5:9f066f9abe5d 139 unsigned char i;
helenh 5:9f066f9abe5d 140
helenh 5:9f066f9abe5d 141
helenh 5:9f066f9abe5d 142 //调试用
helenh 5:9f066f9abe5d 143
helenh 5:9f066f9abe5d 144 if(pserial->readable())
helenh 5:9f066f9abe5d 145 {
helenh 5:9f066f9abe5d 146 //
helenh 5:9f066f9abe5d 147 size=pserial->read(buff,1);
helenh 5:9f066f9abe5d 148 pserial->write(buff,1);
helenh 5:9f066f9abe5d 149
helenh 5:9f066f9abe5d 150 //rw_mutex.lock();
helenh 5:9f066f9abe5d 151
helenh 5:9f066f9abe5d 152 //for(i=0;i<size;i++)
helenh 5:9f066f9abe5d 153 {
helenh 5:9f066f9abe5d 154 //解析加入存储
helenh 5:9f066f9abe5d 155 this->Add_Char(buff[0]);
helenh 5:9f066f9abe5d 156 //
helenh 5:9f066f9abe5d 157 }
helenh 5:9f066f9abe5d 158 //rw_mutex.unlock();
helenh 5:9f066f9abe5d 159 //*/
helenh 5:9f066f9abe5d 160 }
helenh 5:9f066f9abe5d 161
helenh 5:9f066f9abe5d 162 // *pled = !(*pled);
helenh 5:9f066f9abe5d 163
helenh 5:9f066f9abe5d 164
helenh 5:9f066f9abe5d 165
helenh 5:9f066f9abe5d 166 //size=sprintf(this->printbuff,"thread terminal\n");
helenh 5:9f066f9abe5d 167 //printtouart(this->printbuff,size);
helenh 5:9f066f9abe5d 168
helenh 5:9f066f9abe5d 169 }
helenh 5:9f066f9abe5d 170
helenh 5:9f066f9abe5d 171 //线程处理
helenh 5:9f066f9abe5d 172 void Serial_class::Do_Recv_T()
helenh 5:9f066f9abe5d 173 {
helenh 5:9f066f9abe5d 174
helenh 5:9f066f9abe5d 175 //临时存储接收的字符串
helenh 2:a668eb71516b 176 //char buff[RECV_BUFFER_SIZE];
helenh 2:a668eb71516b 177 char tem;
helenh 4:82765e1fd9db 178 int size;
helenh 4:82765e1fd9db 179 sprintf(this->printbuff,"thread begin\n");
helenh 4:82765e1fd9db 180 printtouart(this->printbuff,sizeof(this->printbuff));
helenh 2:a668eb71516b 181 //unsigned char length;
helenh 4:82765e1fd9db 182 unsigned char i;
helenh 0:7f16c88e4047 183 //
helenh 2:a668eb71516b 184 // pc.printf("successful");
helenh 3:57962324a4a0 185
helenh 4:82765e1fd9db 186 //调试用
helenh 3:57962324a4a0 187 while(true)
helenh 3:57962324a4a0 188 {
helenh 4:82765e1fd9db 189 if(pc->readable())
helenh 4:82765e1fd9db 190 {
helenh 4:82765e1fd9db 191 //
helenh 4:82765e1fd9db 192 size=pc->read(recv_buff,20);
helenh 4:82765e1fd9db 193 recv_buff[size+1]='\n';
helenh 4:82765e1fd9db 194 sprintf(this->printbuff,"received char is %s\n",recv_buff);
helenh 4:82765e1fd9db 195 printtouart(this->printbuff,sizeof(this->printbuff));
helenh 4:82765e1fd9db 196 //pc->write("received char is %c",tem);
helenh 4:82765e1fd9db 197 //pc->printf("read successful\n");
helenh 4:82765e1fd9db 198 //Add_Char(tem);
helenh 2:a668eb71516b 199 //rw_mutex.lock();
helenh 4:82765e1fd9db 200 for(i=0;i<size;i++)
helenh 2:a668eb71516b 201 //{
helenh 2:a668eb71516b 202 //解析加入存储
helenh 4:82765e1fd9db 203 this->Add_Char(recv_buff[i]);
helenh 0:7f16c88e4047 204 //
helenh 2:a668eb71516b 205 //}
helenh 2:a668eb71516b 206 //rw_mutex.unlock();
helenh 4:82765e1fd9db 207 }
helenh 4:82765e1fd9db 208 *pled = !(*pled);
helenh 4:82765e1fd9db 209 ThisThread::sleep_for(500);//wait(0.100);
helenh 4:82765e1fd9db 210 //pc->printf("do recv\n");
helenh 3:57962324a4a0 211 }
helenh 4:82765e1fd9db 212
helenh 4:82765e1fd9db 213 //pc->printf("thread terminal");
helenh 4:82765e1fd9db 214 sprintf(this->printbuff,"thread terminal\n");
helenh 4:82765e1fd9db 215 printtouart(this->printbuff,sizeof(this->printbuff));
helenh 2:a668eb71516b 216
helenh 0:7f16c88e4047 217 }
helenh 0:7f16c88e4047 218
helenh 0:7f16c88e4047 219
helenh 0:7f16c88e4047 220 //处理接收到的数据,数据在环形缓存数组内
helenh 0:7f16c88e4047 221 //使用状态机,过滤出帧头,返回信息部分字符串。
helenh 0:7f16c88e4047 222 //输出:pinfor表示的信息字符串,返回值表示当前处理的状态 3:表示收到一个完整的信息。
helenh 2:a668eb71516b 223 /*
helenh 1:09e2d1034894 224 unsigned char Serial_class::DealRecv( unsigned char * pinfor)
helenh 0:7f16c88e4047 225 {
helenh 0:7f16c88e4047 226 //
helenh 0:7f16c88e4047 227 unsigned char result=0;
helenh 0:7f16c88e4047 228 unsigned char recv;
helenh 0:7f16c88e4047 229 //
helenh 0:7f16c88e4047 230 while(IsEmputy()==0)
helenh 0:7f16c88e4047 231 {
helenh 1:09e2d1034894 232 result=this->Read_From(&recv);
helenh 0:7f16c88e4047 233 //
helenh 0:7f16c88e4047 234 if(result==0)
helenh 0:7f16c88e4047 235 return state;
helenh 0:7f16c88e4047 236
helenh 0:7f16c88e4047 237 switch(state)
helenh 0:7f16c88e4047 238 {
helenh 0:7f16c88e4047 239
helenh 0:7f16c88e4047 240 case 0: //判断是否是帧头1
helenh 0:7f16c88e4047 241 int count=0;
helenh 0:7f16c88e4047 242 if(recv==0x99)
helenh 0:7f16c88e4047 243 state=1;
helenh 0:7f16c88e4047 244 break;
helenh 0:7f16c88e4047 245 case 1: //判断是否是帧头2
helenh 0:7f16c88e4047 246 if(recv==0x24)
helenh 0:7f16c88e4047 247 {
helenh 0:7f16c88e4047 248 state=2;
helenh 0:7f16c88e4047 249 count=0; //信息部分计数器初始化
helenh 0:7f16c88e4047 250 }
helenh 0:7f16c88e4047 251 break;
helenh 0:7f16c88e4047 252 case 2: //信息部分,存入数组
helenh 0:7f16c88e4047 253 *pinfor=recv;
helenh 0:7f16c88e4047 254 //是否是字符串结束符
helenh 0:7f16c88e4047 255 if(recv==0x0a)
helenh 0:7f16c88e4047 256 {
helenh 0:7f16c88e4047 257 //
helenh 0:7f16c88e4047 258 state=3;
helenh 0:7f16c88e4047 259 }
helenh 0:7f16c88e4047 260 else
helenh 0:7f16c88e4047 261 {
helenh 0:7f16c88e4047 262 count++;
helenh 0:7f16c88e4047 263 pinfor++;
helenh 0:7f16c88e4047 264 }
helenh 0:7f16c88e4047 265 break;
helenh 0:7f16c88e4047 266 }
helenh 0:7f16c88e4047 267 if(state==3)
helenh 0:7f16c88e4047 268 {
helenh 0:7f16c88e4047 269
helenh 0:7f16c88e4047 270 break;
helenh 2:a668eb71516b 271 }
helenh 0:7f16c88e4047 272 }
helenh 0:7f16c88e4047 273
helenh 0:7f16c88e4047 274 return state;
helenh 0:7f16c88e4047 275 }
helenh 2:a668eb71516b 276 */
helenh 0:7f16c88e4047 277
helenh 2:a668eb71516b 278
helenh 2:a668eb71516b 279 //构造函数
helenh 5:9f066f9abe5d 280 Serial_class::Serial_class(PinName ptx, PinName prx,int baud)
helenh 3:57962324a4a0 281 {
helenh 5:9f066f9abe5d 282 int size=0;
helenh 2:a668eb71516b 283 //创建串口对象s_port
helenh 5:9f066f9abe5d 284 this->s_port =new UnbufferedSerial(ptx,prx,baud);
helenh 2:a668eb71516b 285 //设置串口参数
helenh 5:9f066f9abe5d 286
helenh 5:9f066f9abe5d 287 this->s_port->format(8, BufferedSerial::None, 1);
helenh 5:9f066f9abe5d 288 //this->s_port->enable_input(true);
helenh 5:9f066f9abe5d 289 //this->s_port->enable_output(true);
helenh 3:57962324a4a0 290 //设置初始值
helenh 2:a668eb71516b 291 //this->Init_Infor_struct();
helenh 2:a668eb71516b 292 this->infor_struct.state=0;
helenh 2:a668eb71516b 293 this->infor_struct.count=0;
helenh 2:a668eb71516b 294 this->infor_struct.value=0;
helenh 2:a668eb71516b 295 //关联环形数组
helenh 2:a668eb71516b 296 //this->pcircle_Struct=&circle_Struct;
helenh 2:a668eb71516b 297 //初始化环形数组
helenh 2:a668eb71516b 298 //Init_Circle_Struct(pcircle_Struct);
helenh 5:9f066f9abe5d 299 //pled=new DigitalOut(LED1);
helenh 5:9f066f9abe5d 300 //*pled=1;
helenh 4:82765e1fd9db 301 pc=this->s_port;
helenh 4:82765e1fd9db 302 //
helenh 5:9f066f9abe5d 303 size=sprintf(this->printbuff, "Mbed OS version %d.%d.%d\n",
helenh 4:82765e1fd9db 304 MBED_MAJOR_VERSION,
helenh 4:82765e1fd9db 305 MBED_MINOR_VERSION,
helenh 4:82765e1fd9db 306 MBED_PATCH_VERSION);
helenh 5:9f066f9abe5d 307 printtouart(this->printbuff,size);
helenh 1:09e2d1034894 308
helenh 2:a668eb71516b 309 //关联s_port串口接收中断处理程序为成员函数Do_Recv
helenh 5:9f066f9abe5d 310 s_port->attach(callback(this,&Serial_class::Do_Recv),SerialBase::RxIrq);
helenh 5:9f066f9abe5d 311 //s_port->attach(callback(this,(&Serial_class::Do_Recv_E,s_port)),SerialBase::RxIrq);
helenh 5:9f066f9abe5d 312 //this->m_thread.start(callback(this,&Serial_class::Do_Recv));
helenh 5:9f066f9abe5d 313
helenh 4:82765e1fd9db 314
helenh 1:09e2d1034894 315 }
helenh 1:09e2d1034894 316
helenh 5:9f066f9abe5d 317 /*
helenh 5:9f066f9abe5d 318 void Serial_class::Setcallback()
helenh 5:9f066f9abe5d 319 {
helenh 5:9f066f9abe5d 320 //s_port->attach(callback(this->Do_Recv_E,this->s_port),SerialBase::RxIrq);
helenh 5:9f066f9abe5d 321 this->s_port->attach(callback(this,&Serial_class::Do_Recv),SerialBase::RxIrq);
helenh 5:9f066f9abe5d 322 }
helenh 5:9f066f9abe5d 323 */
helenh 5:9f066f9abe5d 324
helenh 2:a668eb71516b 325 //初始化
helenh 2:a668eb71516b 326 //void Serial_class::Init_Infor_struct(void)
helenh 2:a668eb71516b 327 //{
helenh 2:a668eb71516b 328 // infor_struct.state=4;
helenh 2:a668eb71516b 329 // infor_struct.count=0;
helenh 2:a668eb71516b 330 // infor_struct.value=0;
helenh 2:a668eb71516b 331 //}
helenh 2:a668eb71516b 332
helenh 2:a668eb71516b 333 //析构函数
helenh 2:a668eb71516b 334 Serial_class::~Serial_class()
helenh 0:7f16c88e4047 335 {
helenh 2:a668eb71516b 336 //删除串口对象
helenh 2:a668eb71516b 337 delete this->s_port;
helenh 2:a668eb71516b 338 this->s_port=NULL;
helenh 4:82765e1fd9db 339 //delete this->pc;
helenh 1:09e2d1034894 340 }
helenh 1:09e2d1034894 341
helenh 1:09e2d1034894 342 //
helenh 3:57962324a4a0 343 unsigned int Serial_class::ReadInfor()
helenh 3:57962324a4a0 344 {
helenh 3:57962324a4a0 345 unsigned char result;
helenh 3:57962324a4a0 346 result=infor_struct.value;
helenh 5:9f066f9abe5d 347 infor_struct.state=0;
helenh 5:9f066f9abe5d 348
helenh 3:57962324a4a0 349 return result;
helenh 3:57962324a4a0 350 }
helenh 2:a668eb71516b 351
helenh 4:82765e1fd9db 352
helenh 4:82765e1fd9db 353 void Serial_class::printtouart(char * pb,unsigned char length)
helenh 4:82765e1fd9db 354 {
helenh 4:82765e1fd9db 355 this->s_port->write(pb,length);
helenh 4:82765e1fd9db 356 }
helenh 4:82765e1fd9db 357
helenh 4:82765e1fd9db 358
helenh 2:a668eb71516b 359 //
helenh 2:a668eb71516b 360
helenh 1:09e2d1034894 361
helenh 1:09e2d1034894 362
helenh 1:09e2d1034894 363
helenh 1:09e2d1034894 364