您的位置:首页 > 其它

【智能无线小车系列十一】智能小车一体化测试

2015-05-07 11:05 274 查看
前面介绍了那么多,完成的设计思路是这样的:

使用 树莓派 ,Arduino , 小车底盘 Arduino电机扩展板。摄像头,步进电机, USB无线网卡。

结构如下:PC <---TCP---> 树莓派 <---Serial-->Arduino<---Serial-->步进电机

PC为主控端,树莓派为服务端,Arduino为下位机驱动步进电机。PC做为主控端通过WIFI发送指令给树莓派(服务端),树莓派再将指令通过Serial 发给下位机Arduino控制下边的硬件。摄像头连接树莓派并将视频回传给PC端。后续可以考虑加入手机端控制,试想通过手机来控制小车,并将视频回传给手机,这感觉棒极了!不过这里的手机端尚未实现,不过技术上式完全可行的,实现与否应该就是时间的问题吧。

  

  下面开始进行测试:

  1、Arduino端:下面的代码执行的功能是:通过树莓派传输控制指令控制小车基本运动,具体操作指令如下,数字按键表示的是键盘上的按键。其中,按键8:——前进,按键2——后退,按键4——左转弯,按键6——右转弯,按键5——停止。

将PC通过USB数据线与Arduino连接起来,编译上述代码,然后将其烧制到Arduino板上即可。烧制完成之后,断开数据线。

int pin1=8;

int pin2=9;

int speedpin1=11;

int pin3=6;

int pin4=7;

int speedpin2=10;

char sign;

void setup() {

// put your setup code here, to run once:

pinMode(pin1,OUTPUT);

pinMode(pin2,OUTPUT);

pinMode(speedpin1,OUTPUT);

pinMode(pin3,OUTPUT);

pinMode(pin4,OUTPUT);

pinMode(speedpin2,OUTPUT);

Serial.begin(9600);

}

void loop() {

// put your main code here, to run repeatedly:

if(Serial.available())

{

sign=Serial.read();

switch (sign){

case '6'://right
{

analogWrite(speedpin1,200);//set the PWM speed as 100

analogWrite(speedpin2,200);//set the PWM speed as 100

digitalWrite(pin1,HIGH);

digitalWrite(pin2,LOW);

digitalWrite(pin3,HIGH);

digitalWrite(pin4,LOW);

break;

}

case '2'://backward
{

analogWrite(speedpin1,200);//set the PWM speed as 100

analogWrite(speedpin2,200);//set the PWM speed as 100

digitalWrite(pin1,LOW);

digitalWrite(pin2,HIGH);

digitalWrite(pin3,HIGH);

digitalWrite(pin4,LOW);

break;

}

case '4'://left

{

analogWrite(speedpin1,200);//set the PWM speed as 100

analogWrite(speedpin2,200);//set the PWM speed as 100

digitalWrite(pin1,LOW);

digitalWrite(pin2,HIGH);

digitalWrite(pin3,LOW);

digitalWrite(pin4,HIGH);

break;

}

case '8'://forward

{

analogWrite(speedpin1,200);//set the PWM speed as 100

analogWrite(speedpin2,200);//set the PWM speed as 100

digitalWrite(pin1,HIGH);

digitalWrite(pin2,LOW);

digitalWrite(pin3,LOW);

digitalWrite(pin4,HIGH);

break;

}

case '5'://stop

{

analogWrite(speedpin1,0);//set the PWM speed as 0

analogWrite(speedpin2,0);//set the PWM speed as 0

break;

}

Serial.flush();

}

}

}


2、树莓派:树莓派扮演者服务器的角色。打开树莓派之后,编译并运行OLSR自组网协议。

1)配置网络为ad-hoc网络:

编辑如下的脚本程序,完成后保存为"wlan0.sh"。

#!/bin/sh
#DEV=$1
ifconfig eth0 down
ifconfig wlan0 down
iwconfig wlan0 mode ad-hoc essid 522 channel 3
ifconfig wlan0 up
ifconfig wlan0 192.168.1.52/24
echo 1 > /proc/sys/net/ipv4/conf/all/accept_source_route


  这里需要注意的是,每张网卡由于每次由于所插位置的不同系统为其分配的“wlanX”名中的“X”均不相同,但是只要保持其物理位置不变,该名称也不会再发生变化。因而针对每个位置的每张网卡,在做上述修改之前需要使用命令:iwconfig 查看系统为其指定的名称以替换上述代码中的“wlan0”。不同网卡应当配置不同的ip地址,即每次都需要修改“192.168.1.52/24”中的值。

  由于不希望每次打开树莓派都要重复进行上述配置,因而希望将上述脚本添加到树莓派开机自启动程序列表中。

  不少网友们建议修改"rc.local"文件,然而 "rc.local"的执行是随机的,即不能保证每次开机都能执行,稳定性较差,故而考虑别的方法。比较稳妥的方法之一是执行脚本添加到/etc/init.d目录下。具体操作如下:

首先,切换到root权限,然后将"wlan0.sh"移动至/etc/init.d目录下。

接下来需要修改"wlan0.sh"的执行权限,执行命令:

root@raspiberrypi :/home/pi #chmod 777 /etc/init.d/wlan0.sh


然后把脚本加入到启动清单:

root@raspiberrypi :/home/pi # update-rc.d wlan0.sh defaults


最后重启系统,发现IP地址成功地开机就自动配置为我们设置好的IP地址了。

root@raspiberrypi :/home/pi # reboot


如果希望删除某个开机自启动项,可以使用如下的指令:

pi@raspberrypi $ sudo update-rc.d -f wlan0.sh remove


2)编译/运行olsrd源代码

1.首先从http://www.olsr.org/?q=download下载olsrd源码

2.解压 tar包

tar jxvf olsrd-0.6.6.tar.bz2

3.编译与安装

解压后会生成一个olsrd-0.6.6文件

#cd olsrd-0.6.6 //进入olsrd-0.6.6目录

首先,切换进入olsrd-0.6.8的文件目录,里面的文件目录结构如下所示:

#!/usr/bin/env python
from multiprocessing import Process, Value
import socket
import serial
import time

#"l" respresent an interger,"0" represents the original value
num=Value('l',0)
handle=Value('l',0)
value_change=Value('l',0)

#user self-defined function
def hand(client,count):
while True:
if value_change.value==1 and num.value==count:
print "ready to send"
temp=handle.value
client.send(str(temp))
print "handle send success."
while value_change.value:
temp=0
time.sleep(0.7)
client.close()

#mian function
if __name__ == '__main__':
sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
print "socket create success."
sock.bind (('192.168.1.12',8180))
sock.listen(1)
count=1

while True:
client, addr = sock.accept()
print "connect from :"
print addr
#fork a process to handle a new connecting client
t = Process(target = hand, args = [client,count])
t.start()
#t.join()
print "process create success."

count=count+1

flag=raw_input("all the client connected?(y/n)")
if flag=="y":
#when all the client is connected
#continue to send info to control
#need to change for dymacially connected and disconnected
while True:
value_temp=raw_input("which client do you want to handle :")
handle_temp=raw_input("your handle :")
#get the number of the client need to contorl
num.value=int(value_temp)
#get the information that need to send to the client
handle.value=int(handle_temp)
#set the condition that all the client need to read
value_change.value=1;
#delay for 1 sescond for all the client to read from the shared memory
time.sleep(1)
#cancel the condition that all the client need to read
value_change.value=0;
s.close()


sever.py

参考资料:
http://linux.51yip.com/search/iwconfig
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: