②そしてカメラ台のサーボモーターを制御する。
mjpg-streamer_open.c ストリーミングを開始する。
#include
int main(void){
setuid(0);
system("LD_LIBRARY_PATH=/opt/mjpg-streamer/ /opt/mjpg-streamer/mjpg_streamer -i\"/opt/mjpg-streamer/input_uvc.so -y -r 640x480 -f 15 -d /dev/video0 -y\" -o\"/opt/mjpg-streamer/output_http.so -p 8492 -w www \" -b");
return 0;
}
mjpg-streamer_close.c ストリーミングを終了する。
#include
int main(void){
setuid(0);
system("kill -9 `pidof mjpg_streamer`");
return 0;
}
servo3.py サーボのスイングを開始する。
GPIO18...サーボを左右にスイング
GPIO8....一時停止の割り込み
GPIO7....サーボ終了の割り込み
#!/usr/bin/env python
# coding: utf-8
import pigpio
import time
import sys
PIN0 = 18 # Holizontal swing
# Pin 8 pause Pin 7 stop
pi = pigpio.pi() # default frequency = 50 Hz
pi.set_mode(8, pigpio.INPUT) # button(pin8) input
pi.set_pull_up_down(8, pigpio.PUD_DOWN)
pi.set_mode(7, pigpio.INPUT)
pi.set_pull_up_down(7, pigpio.PUD_DOWN)
pi.set_mode(PIN0, pigpio.OUTPUT)
cnt = 1
def swing_pause(gpio, level, tick):
global cnt
cnt *= -1
time.sleep(0.2)
def swing_stop(gpio, level, tick):
pi.set_mode(PIN0, pigpio.INPUT)
pi.set_mode(8, pigpio.INPUT)
pi.set_mode(7, pigpio.INPUT)
pi.stop()
cb = pi.callback(8, pigpio.RISING_EDGE, swing_pause)
cb = pi.callback(7, pigpio.RISING_EDGE, swing_stop)
try:
d = 1490 # -90deg(560uS) 0deg(1490uS) +90deg(2450uS)
r = 2
while True:
if cnt <= 0:
time.sleep(0.5)
else:
pi.set_servo_pulsewidth(PIN0, d)
time.sleep(0.1)
d += r
if d >= 2405 or d <= 560:
r *= -1
except KeyboardInterrupt:
pass
pi.set_mode(PIN0, pigpio.INPUT)
pi.stop()
pause.py サーボのスイングを一時停止する。GPIO8にパルス出力する。
1回目でスイング一時停止、2回目で再開。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pigpio
from time import sleep
pi = pigpio.pi()
pi.set_mode(8, pigpio.OUTPUT)
pi.write(8, 1)
sleep(0.2)
pi.write(8, 0)
stop.py サーボのスイングを終了する。GPIO7に1回パルスを出力する。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pigpio
from time import sleep
pi = pigpio.pi()
pi.set_mode(7, pigpio.OUTPUT)
pi.write(7, 1)
sleep(0.2)
pi.write(7, 0)
stream5.php ブラウザにコマンドボタンと画像を表示する。
http://192.168.0.10:8080/stream5.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja"> <head>
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Streaming and Still image</title>
<meta name="description" content="" />
<meta name="keywords" content="" />
<link rel="stylesheet" href="stream5.css" type="text/css" media="screen" />
</head>
<body>
<table border="1" width="95%" cellspacing="0" cellpadding="5" bordercolor="#fff8dc">
<tr>
<th><form method="POST" action=""> <input id="submit_button" type="submit" value="ON" name="open" onmouseover="this.style.background='#00ffff'" onmouseout="this.style.background='#000066'"> </form></th>
<th><form method="POST" action=""> <input id="submit_button" type="submit" value="OFF" name="close" onmouseover="this.style.background='#00ffff'" onmouseout="this.style.background='#000066'"> </form></th>
<th><form method="POST" action=""> <input id="submit_button" type="submit" value="start" name="start" onmouseover="this.style.background='#00ffff'" onmouseout="this.style.background='#000066'"> </form></th>
<th><form method="POST" action=""> <input id="submit_button" type="submit" value="pause" name="pause" onmouseover="this.style.background='#00ffff'" onmouseout="this.style.background='#000066'"> </form></th>
<th><form method="POST" action=""> <input id="submit_button" type="submit" value="stop" name="stop" onmouseover="this.style.background='#00ffff'" onmouseout="this.style.background='#000066'"> </form></th>
</tr>
</table>
<?php
if($_POST['open']){
echo 'Start streaming... '.'<br />';
$cmd = './mjpg-streamer_open';
exec($cmd);
}
if($_POST['close']){
echo 'stop streaming... '.'<br />';
$cmd = './mjpg-streamer_close';
exec($cmd);
echo '<img src="./img/course.jpg" width="960" height="700" />';
}
if($_POST['start']){
echo 'Start swing... '.'<br />';
$cmd = 'python servo3.py';
exec($cmd);
}
if($_POST['pause']){
echo 'Pause swing... '.'<br />';
$cmd = 'python pause.py';
exec($cmd);
}
if($_POST['stop']){
echo 'Stop swing... '.'<br />';
$cmd = 'python stop.py';
exec($cmd);
}
?>
<img src="http://192.168.0.10:8492/?action=stream">
</body>
</html>
stream5.css ブラウザの設定。
@charset "shift_jis";
/* 一括で全ての要素の余白をゼロに指定 */
* {
margin: 0;
padding: 0;
}
/*-- ヘッダー部分 --*/
#header {
width: 70%;
height: auto;
margin-bottom: 2px;
position: relative;
background: url(./img/camdata8.jpg) no-repeat 50% 0;
}
h2
{
font-size: 160%;
font-weight: normal;
height: 50px;
padding: 0 0 0 0px;
background: #fff url(img/mark.gif) no-repeat 0 0;
color: #0066ff
}
table.sample1{
border-collapse:collapse;
width:80%;
margin:10px auto;
}
table.sample1 td{
padding:2px;
border:1px solid #ffffff;
}
table.sample1 td{
border-left:#515353 2px ;
border-right:#515353 2px ;
}
.relative {
position: relative;
width: 800px;
}
.absolute {
position: absolute;
right: 30px;
top: 30px;
}
input#submit_button{
width: 150px;
height: 50px;
padding: 2px 5px;
border-radius: 5px;
font-size: 20px;
line-height: 1em;
text-align: center;
color: #ffffff;
background-color: #000066;
transition: .3s;
box-shadow: 6px 6px 3px #666666;
border: 2px solid #000066;
}