2021-02-11 13:11:35 +03:00
|
|
|
namespace modules {
|
2021-02-11 14:05:12 +03:00
|
|
|
/**
|
|
|
|
* A weight measuring sensor.
|
|
|
|
**/
|
|
|
|
//% fixedInstances blockGap=8
|
2021-03-11 17:07:38 +03:00
|
|
|
export class WeightScaleClient extends jacdac.SimpleSensorClient {
|
2022-04-08 18:39:19 +03:00
|
|
|
private readonly _weightError: jacdac.RegisterClient<[number]>
|
|
|
|
private readonly _zeroOffset: jacdac.RegisterClient<[number]>
|
|
|
|
private readonly _gain: jacdac.RegisterClient<[number]>
|
|
|
|
private readonly _maxWeight: jacdac.RegisterClient<[number]>
|
|
|
|
private readonly _minWeight: jacdac.RegisterClient<[number]>
|
|
|
|
private readonly _weightResolution: jacdac.RegisterClient<[number]>
|
|
|
|
private readonly _variant: jacdac.RegisterClient<
|
|
|
|
[jacdac.WeightScaleVariant]
|
|
|
|
>
|
2021-02-23 14:23:30 +03:00
|
|
|
|
|
|
|
constructor(role: string) {
|
2022-04-11 19:26:43 +03:00
|
|
|
super(
|
|
|
|
jacdac.SRV_WEIGHT_SCALE,
|
|
|
|
role,
|
|
|
|
jacdac.WeightScaleRegPack.Weight
|
|
|
|
)
|
2022-04-08 18:39:19 +03:00
|
|
|
|
|
|
|
this._weightError = this.addRegister<[number]>(
|
|
|
|
jacdac.WeightScaleReg.WeightError,
|
2022-04-19 22:08:59 +03:00
|
|
|
jacdac.WeightScaleRegPack.WeightError,
|
|
|
|
jacdac.RegisterClientFlags.Optional
|
2022-04-08 18:39:19 +03:00
|
|
|
)
|
|
|
|
this._zeroOffset = this.addRegister<[number]>(
|
|
|
|
jacdac.WeightScaleReg.ZeroOffset,
|
2022-04-19 22:08:59 +03:00
|
|
|
jacdac.WeightScaleRegPack.ZeroOffset,
|
|
|
|
jacdac.RegisterClientFlags.Optional
|
2022-04-08 18:39:19 +03:00
|
|
|
)
|
|
|
|
this._gain = this.addRegister<[number]>(
|
|
|
|
jacdac.WeightScaleReg.Gain,
|
2022-04-19 22:08:59 +03:00
|
|
|
jacdac.WeightScaleRegPack.Gain,
|
|
|
|
jacdac.RegisterClientFlags.Optional
|
2022-04-08 18:39:19 +03:00
|
|
|
)
|
|
|
|
this._maxWeight = this.addRegister<[number]>(
|
|
|
|
jacdac.WeightScaleReg.MaxWeight,
|
2022-04-19 22:08:59 +03:00
|
|
|
jacdac.WeightScaleRegPack.MaxWeight,
|
|
|
|
jacdac.RegisterClientFlags.Optional |
|
|
|
|
jacdac.RegisterClientFlags.Const
|
2022-04-08 18:39:19 +03:00
|
|
|
)
|
|
|
|
this._minWeight = this.addRegister<[number]>(
|
|
|
|
jacdac.WeightScaleReg.MinWeight,
|
2022-04-19 22:08:59 +03:00
|
|
|
jacdac.WeightScaleRegPack.MinWeight,
|
|
|
|
jacdac.RegisterClientFlags.Optional |
|
|
|
|
jacdac.RegisterClientFlags.Const
|
2022-04-08 18:39:19 +03:00
|
|
|
)
|
|
|
|
this._weightResolution = this.addRegister<[number]>(
|
|
|
|
jacdac.WeightScaleReg.WeightResolution,
|
2022-04-19 22:08:59 +03:00
|
|
|
jacdac.WeightScaleRegPack.WeightResolution,
|
|
|
|
jacdac.RegisterClientFlags.Optional |
|
|
|
|
jacdac.RegisterClientFlags.Const
|
2022-04-08 18:39:19 +03:00
|
|
|
)
|
|
|
|
this._variant = this.addRegister<[jacdac.WeightScaleVariant]>(
|
|
|
|
jacdac.WeightScaleReg.Variant,
|
2022-04-19 22:08:59 +03:00
|
|
|
jacdac.WeightScaleRegPack.Variant,
|
|
|
|
jacdac.RegisterClientFlags.Optional |
|
|
|
|
jacdac.RegisterClientFlags.Const
|
2022-04-08 18:39:19 +03:00
|
|
|
)
|
2021-02-11 13:11:35 +03:00
|
|
|
}
|
2021-02-11 18:13:59 +03:00
|
|
|
|
2021-02-11 13:11:35 +03:00
|
|
|
/**
|
2022-04-08 18:39:19 +03:00
|
|
|
* The reported weight.
|
|
|
|
*/
|
2021-02-23 13:36:16 +03:00
|
|
|
//% callInDebugger
|
2021-02-12 15:35:17 +03:00
|
|
|
//% group="Weight Scale"
|
2022-05-16 22:11:00 +03:00
|
|
|
//% block="%weightscale weight (kg)"
|
2021-02-23 13:36:16 +03:00
|
|
|
//% blockId=jacdac_weightscale_weight___get
|
2021-02-23 14:23:30 +03:00
|
|
|
//% weight=100
|
2021-02-12 15:35:17 +03:00
|
|
|
weight(): number {
|
2022-04-08 18:39:19 +03:00
|
|
|
return this.reading()
|
2021-02-23 13:36:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-08 18:39:19 +03:00
|
|
|
* The estimate error on the reported reading.
|
|
|
|
*/
|
2021-02-23 13:36:16 +03:00
|
|
|
//% callInDebugger
|
|
|
|
//% group="Weight Scale"
|
2021-02-23 14:23:30 +03:00
|
|
|
//% weight=99
|
2021-02-23 13:36:16 +03:00
|
|
|
weightError(): number {
|
2022-04-08 18:39:19 +03:00
|
|
|
this.start()
|
|
|
|
const values = this._weightError.pauseUntilValues() as any[]
|
|
|
|
return values[0]
|
2021-02-23 13:36:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-08 18:39:19 +03:00
|
|
|
* Calibrated zero offset error on the scale, i.e. the measured weight when nothing is on the scale.
|
|
|
|
* You do not need to subtract that from the reading, it has already been done.
|
|
|
|
*/
|
2021-02-23 13:36:16 +03:00
|
|
|
//% callInDebugger
|
|
|
|
//% group="Weight Scale"
|
2021-02-23 14:23:30 +03:00
|
|
|
//% weight=98
|
2021-02-23 13:36:16 +03:00
|
|
|
zeroOffset(): number {
|
2022-04-08 18:39:19 +03:00
|
|
|
this.start()
|
|
|
|
const values = this._zeroOffset.pauseUntilValues() as any[]
|
|
|
|
return values[0]
|
2021-02-23 13:36:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-08 18:39:19 +03:00
|
|
|
* Calibrated zero offset error on the scale, i.e. the measured weight when nothing is on the scale.
|
|
|
|
* You do not need to subtract that from the reading, it has already been done.
|
|
|
|
*/
|
2021-02-23 13:36:16 +03:00
|
|
|
//% group="Weight Scale"
|
2021-02-23 14:23:30 +03:00
|
|
|
//% weight=97
|
2021-02-23 13:36:16 +03:00
|
|
|
setZeroOffset(value: number) {
|
2022-04-08 18:39:19 +03:00
|
|
|
this.start()
|
|
|
|
const values = this._zeroOffset.values as any[]
|
|
|
|
values[0] = value
|
|
|
|
this._zeroOffset.values = values as [number]
|
2021-02-23 13:36:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-08 18:39:19 +03:00
|
|
|
* Calibrated gain on the weight scale error.
|
|
|
|
*/
|
2021-02-23 13:36:16 +03:00
|
|
|
//% callInDebugger
|
|
|
|
//% group="Weight Scale"
|
2021-02-23 14:23:30 +03:00
|
|
|
//% weight=96
|
2021-02-23 13:36:16 +03:00
|
|
|
gain(): number {
|
2022-04-08 18:39:19 +03:00
|
|
|
this.start()
|
|
|
|
const values = this._gain.pauseUntilValues() as any[]
|
|
|
|
return values[0]
|
2021-02-23 13:36:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-08 18:39:19 +03:00
|
|
|
* Calibrated gain on the weight scale error.
|
|
|
|
*/
|
2021-02-23 13:36:16 +03:00
|
|
|
//% group="Weight Scale"
|
2021-02-23 14:23:30 +03:00
|
|
|
//% weight=95
|
2021-02-23 13:36:16 +03:00
|
|
|
setGain(value: number) {
|
2022-04-08 18:39:19 +03:00
|
|
|
this.start()
|
|
|
|
const values = this._gain.values as any[]
|
|
|
|
values[0] = value
|
|
|
|
this._gain.values = values as [number]
|
2021-02-23 13:36:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-08 18:39:19 +03:00
|
|
|
* Maximum supported weight on the scale.
|
|
|
|
*/
|
2021-02-23 13:36:16 +03:00
|
|
|
//% callInDebugger
|
|
|
|
//% group="Weight Scale"
|
2021-02-23 14:23:30 +03:00
|
|
|
//% weight=94
|
2021-02-23 13:36:16 +03:00
|
|
|
maxWeight(): number {
|
2022-04-08 18:39:19 +03:00
|
|
|
this.start()
|
|
|
|
const values = this._maxWeight.pauseUntilValues() as any[]
|
|
|
|
return values[0]
|
2021-02-23 13:36:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-08 18:39:19 +03:00
|
|
|
* Minimum recommend weight on the scale.
|
|
|
|
*/
|
2021-02-23 13:36:16 +03:00
|
|
|
//% callInDebugger
|
|
|
|
//% group="Weight Scale"
|
2021-02-23 14:23:30 +03:00
|
|
|
//% weight=93
|
2021-02-23 13:36:16 +03:00
|
|
|
minWeight(): number {
|
2022-04-08 18:39:19 +03:00
|
|
|
this.start()
|
|
|
|
const values = this._minWeight.pauseUntilValues() as any[]
|
|
|
|
return values[0]
|
2021-02-23 13:36:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-08 18:39:19 +03:00
|
|
|
* Smallest, yet distinguishable change in reading.
|
|
|
|
*/
|
2021-02-23 13:36:16 +03:00
|
|
|
//% callInDebugger
|
|
|
|
//% group="Weight Scale"
|
2021-02-23 14:23:30 +03:00
|
|
|
//% weight=92
|
2021-02-23 13:36:16 +03:00
|
|
|
weightResolution(): number {
|
2022-04-08 18:39:19 +03:00
|
|
|
this.start()
|
|
|
|
const values = this._weightResolution.pauseUntilValues() as any[]
|
|
|
|
return values[0]
|
2021-02-23 13:36:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-08 18:39:19 +03:00
|
|
|
* The type of physical scale
|
|
|
|
*/
|
2021-02-23 13:36:16 +03:00
|
|
|
//% callInDebugger
|
|
|
|
//% group="Weight Scale"
|
2021-02-23 14:23:30 +03:00
|
|
|
//% weight=91
|
2021-03-07 13:38:49 +03:00
|
|
|
variant(): jacdac.WeightScaleVariant {
|
2022-04-08 18:39:19 +03:00
|
|
|
this.start()
|
|
|
|
const values = this._variant.pauseUntilValues() as any[]
|
|
|
|
return values[0]
|
2021-02-23 13:36:16 +03:00
|
|
|
}
|
2021-03-11 17:07:38 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Run code when the weight changes by the given threshold value.
|
2022-04-08 18:39:19 +03:00
|
|
|
*/
|
2021-03-11 17:07:38 +03:00
|
|
|
//% group="Weight Scale"
|
|
|
|
//% blockId=jacdac_weightscale_on_weight_change
|
2022-05-18 22:29:10 +03:00
|
|
|
//% block="on %weightscale weight changed by %threshold (kg)"
|
2021-03-11 17:13:58 +03:00
|
|
|
//% weight=90
|
2021-09-20 18:49:38 +03:00
|
|
|
//% threshold.min=0
|
2021-03-11 17:07:38 +03:00
|
|
|
//% threshold.defl=1
|
|
|
|
onWeightChangedBy(threshold: number, handler: () => void): void {
|
2022-04-08 18:39:19 +03:00
|
|
|
this.onReadingChangedBy(threshold, handler)
|
2021-03-11 17:07:38 +03:00
|
|
|
}
|
2021-02-11 13:11:35 +03:00
|
|
|
|
2021-02-23 14:23:30 +03:00
|
|
|
/**
|
2022-04-08 18:39:19 +03:00
|
|
|
* Call this command when there is nothing on the scale. If supported, the module should save the calibration data.
|
|
|
|
*/
|
2021-02-23 14:23:30 +03:00
|
|
|
//% group="Weight Scale"
|
|
|
|
//% blockId=jacdac_weightscale_calibrate_zero_offset_cmd
|
|
|
|
//% block="%weightscale calibrate zero offset"
|
2021-03-11 17:13:58 +03:00
|
|
|
//% weight=89
|
2021-02-23 14:23:30 +03:00
|
|
|
calibrateZeroOffset(): void {
|
2022-04-08 18:39:19 +03:00
|
|
|
this.start()
|
|
|
|
this.sendCommand(
|
|
|
|
jacdac.JDPacket.onlyHeader(
|
|
|
|
jacdac.WeightScaleCmd.CalibrateZeroOffset
|
|
|
|
)
|
|
|
|
)
|
2021-02-23 14:23:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-08 18:39:19 +03:00
|
|
|
* Call this command with the weight of the thing on the scale.
|
|
|
|
*/
|
2021-02-23 14:23:30 +03:00
|
|
|
//% group="Weight Scale"
|
|
|
|
//% blockId=jacdac_weightscale_calibrate_gain_cmd
|
2021-09-10 23:57:09 +03:00
|
|
|
//% block="%weightscale calibrate gain $weight"
|
2021-03-11 17:13:58 +03:00
|
|
|
//% weight=88
|
2021-02-23 14:23:30 +03:00
|
|
|
calibrateGain(weight: number): void {
|
2022-04-08 18:39:19 +03:00
|
|
|
this.start()
|
|
|
|
this.sendCommand(
|
|
|
|
jacdac.JDPacket.jdpacked(
|
|
|
|
jacdac.WeightScaleCmd.CalibrateGain,
|
2022-04-11 19:26:43 +03:00
|
|
|
jacdac.WeightScaleCmdPack.CalibrateGain,
|
2022-04-08 18:39:19 +03:00
|
|
|
[weight]
|
|
|
|
)
|
|
|
|
)
|
2021-02-23 14:23:30 +03:00
|
|
|
}
|
2021-02-11 13:11:35 +03:00
|
|
|
}
|
2022-06-16 00:56:29 +03:00
|
|
|
|
2022-04-11 19:26:43 +03:00
|
|
|
//% fixedInstance whenUsed weight=1 block="weight scale1"
|
2022-04-08 18:39:19 +03:00
|
|
|
export const weightScale1 = new WeightScaleClient("weight Scale1")
|
|
|
|
}
|