本帖最后由 潇洒哥 于 2013-10-3 23:25 编辑
我的基于arduino和wr703n的wifi监控小车终于搞定了最后一步的串口通讯!由于国庆放假在家,小车放在学校实验室里!没能带回来。所以测试只能用8个LED灯泡代替两块L298N驱动板了!
首先说一下wr703n路由器,我用了论坛大神的固件,地址http://www.wifi-robots.com/thread-1037-1-1.html,固件支持web中文,默认开机启动ser2net和wificar(包括mjpg-streamer)
mjpg-streamer默认是UVC摄像头,跟我的奥尼D811HD720P很相配,视频效果很好,就是fps不是很高,一般在8-10fps左右!
然后就是下位机程序,之前用过论坛管理员的和大神的下位机,感觉有点小BUG,索性参照论坛的下位机自己写了个下位机!原来的BUG也解决了!
以下为我的下位机代码(我用的是两块L298N驱动板,每块电机驱动板上接了两个电机,一共4个电机,4驱动!)
- #include <Servo.h>
- Servo servo1;//定义舵机1
- Servo servo2;//定义舵机2
- //L298N_A
- int M1_1 = 2;
- int M1_2 = 3;
- int M2_1 = 4;
- int M2_2 = 5;
- //L298N_B
- int M3_1 = 8;
- int M3_2 = 9;
- int M4_1 = 10;
- int M4_2 = 11;
- int buffer[3];
- int Serial_flag;
- int temp_char;
- int date_index=0;
- void setup()
- {
- pinMode(2,OUTPUT);
- pinMode(3,OUTPUT);
- pinMode(4,OUTPUT);
- pinMode(5,OUTPUT);
- pinMode(8,OUTPUT);
- pinMode(9,OUTPUT);
- pinMode(10,OUTPUT);
- pinMode(11,OUTPUT);
- servo1.attach(6);//定义舵机1控制接口为数字6
- servo2.attach(7);//定义舵机2控制接口为数字7
- Serial.begin(9600);
- }
- void Stop(void) //四轮停止
- {
- digitalWrite(2,LOW);
- digitalWrite(3,LOW);
- digitalWrite(4,LOW);
- digitalWrite(5,LOW);
- digitalWrite(8,LOW);
- digitalWrite(9,LOW);
- digitalWrite(10,LOW);
- digitalWrite(11,LOW);
- }
- void advance(int x) //向前直行
- {
- digitalWrite(2,LOW);
- digitalWrite(3,HIGH);
- digitalWrite(4,LOW);
- digitalWrite(5,HIGH);
- digitalWrite(8,LOW);
- digitalWrite(9,HIGH);
- digitalWrite(10,LOW);
- digitalWrite(11,HIGH);
- }
- void back(int x) //向后直行
- {
- digitalWrite(2,HIGH);
- digitalWrite(3,LOW);
- digitalWrite(4,HIGH);
- digitalWrite(5,LOW);
- digitalWrite(8,HIGH);
- digitalWrite(9,LOW);
- digitalWrite(10,HIGH);
- digitalWrite(11,LOW);
- }
- void left(int x) //左转
- {
- digitalWrite(2,LOW);
- digitalWrite(3,HIGH);
- digitalWrite(4,LOW);
- digitalWrite(5,HIGH);
- digitalWrite(8,HIGH);
- digitalWrite(9,LOW);
- digitalWrite(10,HIGH);
- digitalWrite(11,LOW);
- }
- void right(int x) //右转
- {
- digitalWrite(2,HIGH);
- digitalWrite(3,LOW);
- digitalWrite(4,HIGH);
- digitalWrite(5,LOW);
- digitalWrite(8,LOW);
- digitalWrite(9,HIGH);
- digitalWrite(10,LOW);
- digitalWrite(11,HIGH);
- }
- void Communication_Decode()
- {
- if(buffer[0]==0x00)
- {
- switch(buffer[1])
- {
- case 0x01:advance(150);return;
- case 0x02:back(150);return;
- case 0x03:right(150);return;
- case 0x04:left(150);return;
- case 0x00:Stop(); return;
- }
- }
- else if(buffer[0]==0x01)
- {
- if(buffer[2]>180)return;
- switch(buffer[1])
- {
- case 0x01:servo1.write(buffer[2]);return;
- case 0x02:servo2.write(buffer[2]);return;
- default:return;
- }
- }
- }
- void loop()
- {
- if(Serial.available() > 0)
- {
- temp_char = Serial.read();
- if(temp_char!=-1)
- {
- if(Serial_flag==0)
- {
- if(temp_char==0xff)
- {
- Serial_flag = 1;
- }
- }
- else
- {
- if(temp_char==0xff)
- {
- Serial_flag = 0;
- if(date_index==3)
- {
- Communication_Decode();
- }
- date_index = 0;
- }
- else
- {
- buffer[date_index]=temp_char;
- date_index++;
- }
- }
- }
- }
- }
复制代码
|