-
Notifications
You must be signed in to change notification settings - Fork 0
/
jm_servo.spin2
137 lines (95 loc) · 4.86 KB
/
jm_servo.spin2
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
'' =================================================================================================
''
'' File....... jm_servo.spin2
'' Purpose.... Smart pin servo control
'' Author..... Jon "JonnyMac" McPhalen
'' Copyright (c) 2020 Jon McPhalen
'' -- see below for terms of use
'' E-mail..... [email protected]
'' Started....
'' Updated.... 30 JUN 2020
''
'' =================================================================================================
{
Simple servo driver that uses a P2 smart pin to create the servo signal. Ramping and other slow
motion behavior of the servo is controlled by the parent application.
}
con { fixed io pins }
RX1 = 63 { I } ' programming / debug
TX1 = 62 { O }
SF_CS = 61 { O } ' serial flash
SF_SCK = 60 { O }
SF_SDO = 59 { O }
SF_SDI = 58 { I }
con
#0, M_ANGLE, M_USECS ' write/read modes
var
long sp ' servo pin
long usecs ' 600..2400
long angle ' 0..180
long setup ' true when pin setup
pub null()
'' This is not a top-level object
pub start(pin, deg) | x
'' Configure smart pin for servo output
'' -- pin is servo output
'' -- deg is position in degrees (0 to 180)
angle := 0 #> deg <# 180 ' keep legal
usecs := angle * 10 + 600 ' convert to us
startx(pin, usecs) ' start servo
pub startx(pin, us) | x
'' Configure smart pin for servo output
'' -- pin is servo output
'' -- us is position in microseconds (600 to 2400)
stop() ' clear old pin
sp := pin ' save pin
x.word[0] := clkfreq / 1_000_000 ' set unit timing (1us)
x.word[1] := 20_000 ' set period (20ms)
usecs := 600 #> us <# 2400 ' keep position legal
pinstart(pin, P_OE | P_PWM_SAWTOOTH, x, usecs) ' servo output
setup := true ' mark setup
pub stop()
'' Disable servo smart pin if previously configured
if (setup)
pinclear(sp) ' disable smart pin
pinfloat(sp) ' release output driver
longfill(@usecs, 0, 3) ' mark disabled
pub write(mode, value)
'' Update servo
'' -- mode is 0 for degrees, 1 for microseconds
'' -- value is new angle or position value
if (setup)
if (mode == M_ANGLE)
angle := 0 #> value <# 180 ' legal degrees
usecs := angle * 10 + 600 ' convert to us
wypin(sp, usecs) ' update servo
elseif (mode == M_USECS)
usecs := 600 #> value <# 2400 ' legal microseconds
angle := (usecs - 600) / 10 ' convert to angle
wypin(sp, usecs) ' update servo
pub read(mode) : result
if (setup)
if (mode == M_ANGLE)
return angle
else
return usecs
else
return 0
con { license }
{{
Terms of Use: MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
}}