-
Notifications
You must be signed in to change notification settings - Fork 1
/
mycar.lua
166 lines (128 loc) · 4.17 KB
/
mycar.lua
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
-- incializalas
function init()
servo.step = (servo.max-servo.min)/180
-- servo setup
-- we do some cheating here: frequency is set be the double than
-- required by the servo. So we send out two signals in one period.
-- My servo safely ignores the second pulse, and this way we get
-- double precion on the pulse length
pwm.setup(servo.pin, 100, (servo.max + servo.min) / 2)
pwm.start(servo.pin)
-- motor setup
motor.pwm = motor.pinA
pwm.setup(motor.pinA, 100, 0)
pwm.setup(motor.pinB, 100, 0)
pwm.start(motor.pinA)
pwm.start(motor.pinB)
-- lights setup
gpio.mode(lights.pin, gpio.OUTPUT);
gpio.write(lights.pin, gpio.LOW);
-- turn signal setup
for i=1,2 do
gpio.mode(signal.pins[i], gpio.OUTPUT)
gpio.write(signal.pins[i], gpio.LOW)
end
wifi.sta.eventMonReg(wifi.STA_GOTIP, onWifiConnected)
wifi.sta.eventMonStart()
initServer()
startVccMonitor()
end
-- server initialization
function initServer()
server.instance = net.createServer(net.TCP, 28800)
server.instance:listen(server.port, function(conn)
print("RoboRemo connected")
table.insert(server.conn, conn)
onConnect()
-- Turn on servo
gpio.write(servo.enable, gpio.LOW)
conn:on("receive", receiveData)
conn:on("disconnection", function(conn)
print("RoboRemo disconnected")
for i=table.getn(server.conn), 1, -1 do
if (conn == server.conn[i]) then
table.remove(server.conn, i)
end
end
onDisconnect()
end)
end)
end
-- parse commands
function parseCmd(str, conn)
local start, finish, cmd, value = string.find(str, "(%w+) (-?[.%d]+)")
if (start ~= 1 or finish < string.len(str)-1) then
print ("Invalid input: "..str)
return
end
value = tonumber(value)
local ret = nil
if (cmd == "turn") then
if (value < -8) then value = -8
elseif (value > 8) then value = 8
end
turn(value * servo.korrB + servo.center) -- this value is between -10 and + 10 (sent by roboremo)
elseif (cmd == "speed") then setSpeed(value)
elseif (cmd == "reverse") then setReverse(value)
elseif (cmd == "lights") then ret = toggleLights(value)
else
print ("Invalid command: "..cmd)
end
if (ret) then
conn:send(ret)
end
end
-- new message has been received
-- concatenate all input until a newline character has been received
-- then the string before the newline is handed to parseCmd
-- if there where multiple newlines in one data packet
-- then each of them has to be handled
function receiveData(conn, data)
server.buff = server.buff .. data
local a, b = string.find(server.buff, "\n", 1, true)
while a do
parseCmd( string.sub(server.buff, 1, a-1), conn )
server.buff = string.sub(server.buff, a+1, string.len(server.buff))
a, b = string.find(server.buff, "\n", 1, true)
end
end
-- Move servo to a given position
-- pos: the requested servo position between -90 and 90 degrees
function turn(pos)
if (pos < -90 or pos > 90) then
return
end
local target = math.floor((pos + 90) * servo.step + servo.min)
pwm.setduty(servo.pin, target)
autoSignal(pos)
end
-- set the speed
-- speed: the desired speed between 0 and 100
function setSpeed(speed)
if (speed > 100) then
speed = 100
elseif (speed < 10) then
speed = 0 -- too low values would just stress the motor, but won't be able to turn it
end
--utana a bekapcsolasok
local val = math.floor(1023 / 100 * math.abs(speed))
pwm.setduty(motor.pwm, val)
end
-- switch between forward and reverse
-- reverse: if true then we will go reversed
function setReverse(reverse)
pwm.setduty(motor.pwm, 0)
if (reverse == 1) then
motor.pwm = motor.pinB
else
motor.pwm = motor.pinA
end
end
-- turn the lighting on/off
-- first call turn on the lighting, the second turns it off
function toggleLights(value)
lights.on = 1 - lights.on
gpio.write(lights.pin, lights.on)
return "lights "..lights.on.."\n"
end
init()