Hardware Reference
In-Depth Information
// Set the eEQP period, convert to ns.
fs
.
writeFile
(
eQEP
+
'period'
,
period
*
1000000
,
function
(
err
) {
if
(
err
)
throw
err
;
console
.
log
(
'Period updated to '
+
period
*
1000000
);
})
// Enable
fs
.
writeFile
(
eQEP
+
'enabled'
,
1
,
function
(
err
) {
if
(
err
)
throw
err
;
console
.
log
(
'Enabled'
);
})
setInterval
(
readEncoder
,
period
);
// Check state every 250 ms
function
readEncoder
(
x
) {
fs
.
readFile
(
eQEP
+
'position'
, {
encoding
:
'utf8'
},
printValue
);
}
function
printValue
(
err
,
data
) {
if
(
err
)
throw
err
;
if
(
oldData
!==
data
) {
console
.
log
(
'position: '
+
data
+
' speed:
'
+(
oldData
-
data
));
oldData
=
data
;
}
}
Try rotating the encoder clockwise and counter-clockwise. You'll see an output like this:
Period updated to 100000000
Enabled
position: 0
speed: NaN
position: 4
speed: -4
position: 6
speed: -2
position: 8
speed: -2
position: 12
speed: -4
position: 16
speed: -4
position: 19
speed: -3