-
Notifications
You must be signed in to change notification settings - Fork 0
/
speed.js
49 lines (42 loc) · 1.62 KB
/
speed.js
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
// Calculate speed from Microbit acceleration
// NOT CALIBRATED !
let averageAccelerationMss = 0
let averageAccelerationMilliGs = 0
let samples = 0
let totalAcceleration = 0
let endTime = 0
let speed = 0
let maxAccelerationMilligs = 0
let maxAccelerationMss = 0
let sampleTimeMs = 500
basic.forever(function () {
while (true) {
endTime = input.runningTime() + sampleTimeMs
totalAcceleration = 0
samples = 0
// Average acceleration over `sampleTimeMs`
while (input.runningTime() < endTime) {
totalAcceleration += input.acceleration(Dimension.Strength) - 1024
samples += 1
}
averageAccelerationMilliGs = totalAcceleration / samples
// Convert to metres per second squared
averageAccelerationMss = averageAccelerationMilliGs * 9.80665 / 1024
// Record maximums
maxAccelerationMilligs = Math.max(maxAccelerationMilligs, averageAccelerationMilliGs)
maxAccelerationMss = Math.max(maxAccelerationMss, averageAccelerationMss)
// Assuming a standing start and a constant acceleration in the same direction - horrible!
// An improvement would be to calculate this in each of the three directions, but how deal with the microbit spinning in space?
speed = averageAccelerationMs * sampleTimeMs / 1000 + speed
basic.pause(100)
}
})
input.onButtonPressed(Button.A, function () {
basic.showNumber(maxAccelerationMs)
})
input.onButtonPressed(Button.AB, function () {
basic.showNumber(maxAccelerationMilligs)
})
input.onButtonPressed(Button.B, function () {
basic.showNumber(Math.floor(speed))
})