forked from shaunuk/picar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.html
executable file
·114 lines (89 loc) · 2.65 KB
/
socket.html
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
<html>
<head>
<script src="jquery-2.0.3.min.js" language="javascript"></script>
<script src="/socket.io/socket.io.js"></script>
<meta name="viewport" content="user-scalable=no, initial-scale=1.0, maximum-scale=1.0;" />
<script>
//------ Define your variables here
var socket = io.connect(window.location.hostname+':8080');
var nIntervId;
//steering min and max pwm cmds
var maxbeta = .175; //r limit
var minbeta = .105; //l limit
//throttle min and max pwm cmds
var mingamma = .105; //backwards limit
var maxgamma = .175; //forward limit
// This function is called when the page 1st loads on your phone
//
$(function()
{
//send data to the pi at 20Hz
nIntervId = setInterval(SendToPi ,50);
window.gyro = 'ready';
alert('Ready -- Lets race !');
});
// This fuction is called @ 20Hz
//
function SendToPi()
{
if (window.gyro == 'ready')
{
//dont send another command until last one doone... its only a little pi
window.gyro = 'notready';
socket.emit('fromclient', { beta: beta, gamma: gamma } );
window.gyro = 'ready';
}//END if
}//End SendToPI
// This fuction is called when the phone orientation changes
//
window.ondeviceorientation = function(event)
{
var tmp_beta, tmp_gamma;
//steer
tmp_beta = .001167 * event.beta + .14;
//iphone beta = .001167 * event.beta * -1) + .14;
if (tmp_beta >= maxbeta)
{
tmp_beta=maxbeta;
}
if (tmp_beta <= minbeta)
{
tmp_beta=minbeta;
}
beta = tmp_beta; //update global
// throttle...
// rather than going 0-360 gamma counts in + or - 90 degree quadrants.
// this can be a pain as if you go past 90 degrees it can start
// going the full opposite dir!
// past -90 (full reverse) it count down from + 90 (which would cause car to go full fwds)
// below 0 (full fwds) it counts up (which would get ignored)
// this will just confuse the operator so we'll mask i out so it stays in the 0- (-90) range
tmp_gamma = event.gamma;
if(tmp_gamma > 45)
{
tmp_gamma = -90; // gone past full reverse
}
else if (tmp_gamma > 0)
{
tmp_gamma = 0; //gone past full fwds
}
tmp_gamma = .00125 * tmp_gamma + .175;
//iphone gamma = -0.00125 * Math.round(event.gamma) + .175;
if (tmp_gamma >= maxgamma)
{
tmp_gamma=maxgamma;
}
if (tmp_gamma <= mingamma)
{
tmp_gamma=mingamma;
}
gamma = tmp_gamma; //update global
}//END ondviceorientation
</script>
<body bgcolor="teal">
</head><body>
<div id=info><font size=7><pre></pre>picar</pre></font></div>
<div id=info><font size=3><pre></pre>https://github.com/lawsonkeith/picar</pre></font></div>
</body>
</head>
</html>