树莓派智能小车WEB遥控器代码

2016-8-19 Linyuting.cn

    记录下几个月前搞的树莓派遥控小车(两驱)的代码,因为我加了个转向灯,所以这里是包含转向灯的代码,GPIO口对应插好即可。

    注意这里是树莓派搭建好Nginx + PHP环境,不要禁用了system函数,通过IP地址访问到该遥控器来进行控制。

    进程通讯主要是解决转向灯(LED发光二极管)闪烁问题,比如当我们左转向时,左转向灯会进入一个闪烁的状态,在while循环中执行,通过设置0和1来控制转向灯的熄灭与亮起,但是这样就会遇到一个问题,当我们再按右转时,左转向灯就停不下来了,左右转向灯一起闪烁了,因为他一直处于while循环之中。而我们左右转向两次请求的PHP脚本他们之间无法进行直接的交流,所以这里使用进程通讯,当按下另一个转向时,我们发送message让前面的while循环不再继续执行。

    遥控器图片controler.png


<?php

//马达相关
$pin1 = 0;
$pin2 = 3;
$pin3 = 4;
$pin4 = 5;

//转向灯
$pinL = 25;
$pinR = 27;

ignore_user_abort(false);

// 设置进程通讯用来控制后面的方向灯
$message_queue_key = ftok(__FILE__, 'a');
$message_queue = msg_get_queue($message_queue_key, 0666);

/*
* 控制pin
* 参数:pin1的值   pin2的值
*/
function GPIO_Exec($pin1val,$pin2val,$pin3val,$pin4val){

	global $pin1,$pin2,$pin3,$pin4;

	system("gpio mode $pin1 OUTPUT");
	system("gpio write $pin1 $pin1val");

	system("gpio mode $pin2 OUTPUT");
	system("gpio write $pin2 $pin2val");


	system("gpio mode $pin3 OUTPUT");
	system("gpio write $pin3 $pin3val");

	system("gpio mode $pin4 OUTPUT");
	system("gpio write $pin4 $pin4val");

	return true;

}

/*
* 控制pin
* 参数:pin1的值   pin2的值
*/
function GPIO_Exec_Light($pinLval,$pinRval){

	global $pinL,$pinR,$message_queue;

	if($pinLval!=$pinRval){
		if($pinLval==1 && $pinRval==0){
			system("gpio mode $pinL OUTPUT");
			$temp = 1;
			$message = '';
			do{
				system("gpio write $pinL $temp");
				usleep(500000);
				$temp = $temp==1?0:1;
				msg_receive($message_queue, 0, $message_type, 1024, $message, true, MSG_IPC_NOWAIT);
			}while($message=='l' || $message=='');
			system("gpio write $pinL 0");
			exit();
		}
		if($pinLval==0 && $pinRval==1){
			system("gpio mode $pinR OUTPUT");
			$temp = 1;
			$message = '';
			do{
				system("gpio write $pinR $temp");
				usleep(500000);
				$temp = $temp==1?0:1;
				msg_receive($message_queue, 0, $message_type, 1024, $message, true, MSG_IPC_NOWAIT);
			}while ($message=='r' || $message=='');
			system("gpio write $pinR 0");
			exit();
		}
	}

	return true;

}

if(isset($_GET['action'])){

	$action = trim($_GET['action']);
	msg_receive($message_queue, 0, $message_type, 1024, $message, true, MSG_IPC_NOWAIT);
	switch ($action) {
		case 'up':
			msg_send($message_queue, 1, "0");
			GPIO_Exec(1,0,1,0);
			GPIO_Exec_Light(0,0);
			break;
		case 'down':
			msg_send($message_queue, 1, "0");
			GPIO_Exec(0,1,0,1);
			GPIO_Exec_Light(0,0);
			break;
		case 'left':
			msg_send($message_queue, 1, "l");
			GPIO_Exec(0,0,1,0);
			GPIO_Exec_Light(1,0);
			break;
		case 'right':
			msg_send($message_queue, 1, "r");
			GPIO_Exec(1,0,0,0);
			GPIO_Exec_Light(0,1);
			break;
		case 'stop':
			msg_send($message_queue, 1, "0");
			GPIO_Exec(0,0,0,0);
			GPIO_Exec_Light(0,0);
			break;
		case 'reboot':
			exec("sudo reboot", $out);
			print_r($out);
	}


	exit('success');
}

?>
<!doctype html>
<html>
<head>
	<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
	<meta charset="utf-8" />
	<title>遥控器</title>
	<style type="text/css">
	html,body{
		height: 100%;
		padding: 0;
		margin: 20px 0px;
		overflow-x: hidden;
		overflow-y: hidden;
	}
	#mapimg{
		display: block;
		margin: 0 auto;
		width: 360px;
		margin: 0 auto;
		overflow: hidden;
	}
	area{
		cursor: pointer;
	}
	.rebootbtn{
		position: absolute;
		left: 3px;
		bottom: 3px;
		cursor: pointer;
	}
	</style>
</head>
</html>
<body>
	<img src ="controler.png" alt="Planets" usemap ="#mapblock" id="mapimg" name="mapimg" />
	<map id ="mapblock" name="mapblock">
		<area shape="poly" coords="125,133,41,62,111,7,267,7,333,55,256,131,234,114,216,108,193,103,171,105,143,115,130,130,125,133" onclick="go('up');" alt="up" />
		<area shape="poly" coords="128,250,155,272,189,281,215,278,259,249,336,316,262,369,226,378,153,378,95,359,52,326,126,252" onclick="go('down')" alt="down" />
		<area shape="poly" coords="46,68,121,136,108,175,108,213,126,248,50,321,6,258,12,110,38,65" onclick="go('left')" alt="left"></area>
		<area shape="poly" coords="258,132,331,57,369,119,371,256,335,315,263,244,280,192,257,133" onclick="go('right')" alt="right"></area>
		<area shape ="circle" coords ="193,193,86" onclick="go('stop');" alt="Stop" />
	</map>
	<span onclick="go('reboot');" class="rebootbtn">Reboot</span>
	<img src="" border="0" id="ajaximg" name="ajaximg" style="display:none;">
	<script type="text/javascript">
		// document.getElementById('mapimg').onclick = function(e){
		// 	var x = e.clientX - mapimg.offsetLeft;
		// 	var y = e.clientY - mapimg.offsetTop - 20;
		// 	datas.value = datas.value + x + "," + y + ",";
		// 	console.log(x+","+y);
		// }
		document.body.addEventListener('touchmove', function (event) {
		    event.preventDefault();
		}, false);
		document.body.addEventListener('touchstart', function (event) {
		    
		    var x = event.targetTouches[0].pageX, y = event.targetTouches[0].pageY;
		    var ele = document.createElement("div");
		    ele.style = "position: absolute; top:"+y+"px; left:"+x+"px; width:30px; height:30px; background-color:#000; border-radius:30px;";
		    document.body.appendChild(ele);
		    
		}, false);
		function go(command){
			ajaximg.src = "?action="+command;
		}
	</script>
</body>
</html>


标签: php 树莓派

发表评论:

本站由emlog驱动 粤ICP备15042739号