-
Notifications
You must be signed in to change notification settings - Fork 0
/
controld.php
200 lines (148 loc) · 4.17 KB
/
controld.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?
/*
controld.php
control sparky robot servos through makeboard and osc
Author: John Celenza
*/
require("hdf.php");
require("OSC.phps");
/* get_control_arg
take control argument in the form "_reverse0_<value>"
returns value
*/
function get_control_arg($value_str)
{
$value = "";
$args = explode("_",$value_str);
$value = $args[2];
echo "get_control_arg value_str:$value_str value:$value\n";
return $value;
}
function sc_move_dir($c,$dir_with_args,$config)
{
echo "$dir\n";
$c->send(new OSCMessage("/appled/1/state", array(1)));
$dir_args = explode("_",$dir_with_args);
if (strstr($dir_with_args,"_slow"))
$slow_str = "_slow";
else
$slow_str = "";
$sleep_length = 2;
//_time_<seconds>
if (($time_val_str = strstr($dir_with_args,"_time")))
{
$sleep_length = substr($time_val_str,6,1);
echo "Got a time $time_val_str sleep_length:$sleep_length\n";
}
$dir = "$dir_args[0]_$dir_args[1]$slow_str";
echo "primary_command:$dir\n";
$servo_0 = -1;
$servo_1 = -1;
if ($val_str = strstr($dir_with_args,"_straight0"))
$servo_0 = get_control_arg($val_str);
else if ($val_str = strstr($dir_with_args,"_reverse0"))
$servo_0 = get_control_arg($val_str);
if ($val_str = strstr($dir_with_args,"_straight1"))
$servo_1 = get_control_arg($val_str);
else if ($val_str = strstr($dir_with_args,"_reverse1"))
$servo_1 = get_control_arg($val_str);
if ($servo_0 < 0)
$servo_0 = hdf_get_value($config,"movement.$dir.0",0);
if ($servo_1 < 0)
$servo_1 = hdf_get_value($config,"movement.$dir.1",0);
$stop_0 = hdf_get_value($config,"movement.stop.0",0);
$stop_1 = hdf_get_value($config,"movement.stop.1",0);
if ($dir_with_args=="stop")
{
$servo_0 = $stop_0;
$servo_1 = $stop_1;
}
echo "sc_move_dir:CONTROL::::::::::::::::::::::::::::::::::::::\n";
echo "sc_move_dir:servo_0:".intval($servo_0)."\n";
echo "sc_move_dir:servo_1:".intval($servo_1)."\n";
echo "sc_move_dir:stop_0: $stop_0\n";
echo "sc_move_dir:stop_1: $stop_1\n";
echo "sleep_length:$sleep_length\n";
$c->send(new OSCMessage("/servo/1/position", array(intval($servo_0))));
$c->send(new OSCMessage("/servo/0/position", array(intval($servo_1))));
//sleep($sleep_length);
//$c->send(new OSCMessage("/servo/0/position", array(intval($stop_0))));
//$c->send(new OSCMessage("/servo/1/position", array(intval($stop_1))));
//$c->send(new OSCMessage("/appled/1/active", array(0)));
$c->send(new OSCMessage("/appled/1/state", array(0)));
return;
}
////////////////////////////
////MAIN
////////////////////////////
$config = hdf_read_file("control.hdf");
$last_command_epoch = time();
$last_action = "stop";
while (1)
{
`touch /tmp/SparkyController.lock; chmod a+rw /tmp/SparkyController.lock`;
$c = new OSCClient();
$c->set_destination("192.168.0.200", 10000);
$action = "";
$mystat = 0;
$mystat = stat("/tmp/SparkyController.lock");
$in_action=fopen("/tmp/SparkyController.lock","r");
if ($in_action)
{
$action = ltrim(rtrim(fgets($in_action)));
$last_command_epoch = fgets($in_action);
echo "READ last_command_epoch: $last_command_epoch\n";
if (!strlen($last_command_epoch))
$last_command_epoch = 0;
fclose($in_action);
if ($last_action != $action)
{
//echo "setting last_command_epoch".time();
$last_action = $action;
$last_command_epoch = time();
$time_elapsed = 0;
}
else
{
$time_elapsed = time(NULL) - $last_command_epoch;
}
//don't write again
if ($action=="stop")
{
$time_elapsed = 0;
}
if ($time_elapsed > hdf_get_value($config,"control.timeout",1) && $action!="stop")
{
//echo "control timeout: setting stop time_elapsed: $time_elapsed last_command_epoch:$last_command_epoch time:".time()."\n";
$action = "stop";
if (1)
{
umask(0777);
$out_action = fopen("/tmp/SparkyController.lock","w");
if ($out_action)
{
fputs($out_action,"$action");
fclose($out_action);
}
}
}
}
$show_ui = 1;
if (strlen($action) && $action!=$last_action_hero)
{
$show_ui = 0;
echo "$action\n";
/*
if ($action=="stop")
{
//echo "Sending STOP 2x\n";
SC_move_dir($c,$action,$config);
usleep(100000);
}
*/
//echo "Sending ACTION $action to SC_move_dir\n";
SC_move_dir($c,$action,$config);
}
$last_action_hero = $action;
usleep(100000);
};