forked from zeroflag/punyforth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample-geekcreit-rctank.forth
172 lines (146 loc) · 3.95 KB
/
example-geekcreit-rctank.forth
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
\ this demo is for http://bit.ly/2bqHz58
5 constant: PIN_SPEED_1 \ D1
4 constant: PIN_SPEED_2 \ D2
0 constant: PIN_MOTOR_1 \ D3
2 constant: PIN_MOTOR_2 \ D4
14 constant: PIN_LIGHT_1 \ D5
15 constant: PIN_LIGHT_2 \ D8
create: PWM_PINS PIN_SPEED_1 c, PIN_SPEED_2 c,
FALSE init-variable: lamp-active
: lamp-on ( -- )
PIN_LIGHT_1 GPIO_OUT gpio-mode
PIN_LIGHT_2 GPIO_OUT gpio-mode
PIN_LIGHT_1 GPIO_HIGH gpio-write
PIN_LIGHT_2 GPIO_HIGH gpio-write ;
: lamp-off ( -- )
PIN_LIGHT_1 GPIO_OUT gpio-mode
PIN_LIGHT_2 GPIO_OUT gpio-mode
PIN_LIGHT_1 GPIO_LOW gpio-write
PIN_LIGHT_2 GPIO_LOW gpio-write ;
: lamp-toggle ( -- )
lamp-active @ if lamp-off else lamp-on then
lamp-active @ invert lamp-active ! ;
: engine-start ( -- )
PIN_MOTOR_1 GPIO_OUT gpio-mode
PIN_MOTOR_2 GPIO_OUT gpio-mode
PWM_PINS 2 pwm-init
1000 pwm-freq
1023 pwm-duty
pwm-start ;
: engine-stop ( -- ) pwm-stop ;
: forward ( -- v1 v2 ) GPIO_LOW GPIO_LOW ;
: back ( -- v1 v2 ) GPIO_HIGH GPIO_HIGH ;
: left ( -- v1 v2 ) GPIO_LOW GPIO_HIGH ;
: right ( -- v1 v2 ) GPIO_HIGH GPIO_LOW ;
: direction ( v1 v2 -- )
PIN_MOTOR_2 swap gpio-write
PIN_MOTOR_1 swap gpio-write ;
30000 constant: very-slow
40000 constant: slow
50000 constant: medium
60000 constant: fast
65535 constant: full
medium init-variable: current-speed
: speed ( n -- )
case
0 of
pwm-stop
PIN_SPEED_1 GPIO_LOW gpio-write
PIN_SPEED_2 GPIO_LOW gpio-write
endof
full of
pwm-stop
PIN_SPEED_1 GPIO_HIGH gpio-write
PIN_SPEED_2 GPIO_HIGH gpio-write
endof
pwm-duty
pwm-start
endcase ;
: brake ( -- ) 0 speed ;
\ Distance sensor setup
13 constant: PIN_TRIGGER \ D7
12 constant: PIN_ECHO \ D6
100 constant: MAX_CM
20 constant: MIN_CM
: distance ( -- cm | MAX_CM )
{ PIN_ECHO MAX_CM cm>timeout PIN_TRIGGER ping pulse>cm }
catch dup ENOPULSE = if
drop MAX_CM
else
throw
then ;
: obstacle? ( -- bool ) distance MIN_CM < ;
: turn ( -- )
right direction current-speed @ speed
50 ms ;
: go ( -- )
forward direction current-speed @ speed
50 ms ;
: auto-pilot ( -- )
begin
begin
obstacle?
while
turn
repeat
go
again ;
8000 constant: PORT
PORT wifi-ip netcon-udp-server constant: server-socket
1 buffer: command
: command-loop ( task -- )
activate
begin
server-socket 1 command netcon-read
-1 <>
while
command c@
case
[ char: F ] literal of
forward direction current-speed @ speed
endof
[ char: B ] literal of
back direction current-speed @ speed
endof
[ char: L ] literal of
left direction current-speed @ speed
endof
[ char: R ] literal of
right direction current-speed @ speed
endof
[ char: S ] literal of
brake
endof
[ char: I ] literal of
current-speed @ 10 + full min
current-speed !
current-speed @ speed
endof
[ char: D ] literal of
current-speed @ 10 - 0 max
current-speed !
current-speed @ speed
endof
[ char: E ] literal of
engine-start
endof
[ char: H ] literal of
engine-stop
endof
[ char: T ] literal of
lamp-toggle
endof
[ char: A ] literal of
auto-pilot
endof
endcase
repeat
deactivate ;
0 task: tank-task
: tank-server-start ( -- )
multi
engine-start
6 0 do lamp-toggle 200 ms loop
tank-task command-loop ;
repl-start
tank-server-start