Skip to content

Commit

Permalink
do not use default values for rendering servo; rely on min/max angle
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Oct 18, 2023
1 parent 749f29b commit 279473a
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion jacdac-python
2 changes: 1 addition & 1 deletion jacdac-ts
Submodule jacdac-ts updated 1 files
+1 −1 jacdac-spec
2 changes: 1 addition & 1 deletion pxt-jacdac
23 changes: 18 additions & 5 deletions src/components/dashboard/DashboardServo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import { ServoServer } from "../../../jacdac-ts/src/servers/servoserver"
import { JDService } from "../../../jacdac-ts/src/jdom/service"
import ServoWidget from "../widgets/ServoWidget"
import useRegister from "../hooks/useRegister"
import DashboardRegisterValueFallback from "./DashboardRegisterValueFallback"

function useActualAngle(service: JDService, visible: boolean) {
const angleRegister = useRegister(service, ServoReg.Angle)
const [angle = 90] = useRegisterUnpackedValue<[number]>(angleRegister, {
const [angle] = useRegisterUnpackedValue<[number]>(angleRegister, {
visible,
})
const actualAngleRegister = useRegister(service, ServoReg.ActualAngle)
Expand Down Expand Up @@ -52,25 +53,33 @@ export default function DashboardServo(props: DashboardServiceProps) {
props
)
const minAngleRegister = useRegister(service, ServoReg.MinAngle)
const [minAngle = -90] = useRegisterUnpackedValue<[number]>(
const [minAngle] = useRegisterUnpackedValue<[number]>(
minAngleRegister,
props
)
const maxAngleRegister = useRegister(service, ServoReg.MaxAngle)
const [maxAngle = 90] = useRegisterUnpackedValue<[number]>(
const [maxAngle] = useRegisterUnpackedValue<[number]>(
maxAngleRegister,
props
)
const responseSpeedRegister = useRegister(service, ServoReg.ResponseSpeed)
const [responseSpeed = SG90_RESPONSE_SPEED] = useRegisterUnpackedValue<
[number]
>(responseSpeedRegister, { visible })
const server = useServiceServer<ServoServer>(service)

if (angle === undefined)
return <DashboardRegisterValueFallback register={angleRegister} />
if (minAngle === undefined)
return <DashboardRegisterValueFallback register={minAngleRegister} />
if (maxAngle === undefined)
return <DashboardRegisterValueFallback register={maxAngleRegister} />

const rotationalSpeed = 60 / responseSpeed

const continuous = /cont=1/.test(clientVariant)
const throttle = ((angle - minAngle) / (maxAngle - minAngle)) * 200 - 100

const server = useServiceServer<ServoServer>(service)
const color = server ? "secondary" : "primary"

const toggleOff = () => enabledRegister.sendSetBoolAsync(!enabled, true)
Expand Down Expand Up @@ -104,7 +113,11 @@ export default function DashboardServo(props: DashboardServiceProps) {
enabled={enabled}
toggleOff={toggleOff}
widgetSize={widgetSize}
rotationRate={continuous ? (throttle * rotationalSpeed) / 100 : undefined}
rotationRate={
continuous
? (throttle * rotationalSpeed) / 100
: undefined
}
visible={visible}
/>
</Grid>
Expand Down
5 changes: 5 additions & 0 deletions src/components/hooks/useThrottledValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ export default function useThrottledValue(
return false
}

// not a value yet
if (isNaN(value)) {
return false
}

// increment towards value
const dt = time / 1000 // s
const error = value - animated
Expand Down

0 comments on commit 279473a

Please sign in to comment.