Skip to content

Commit

Permalink
fix simulation for gyro/acc
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Nov 6, 2023
1 parent 5978203 commit 7129d81
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 35 deletions.
46 changes: 38 additions & 8 deletions packages/drivers/src/accelerometer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "@devicescript/server"
import { AccelerometerServerSpec } from "@devicescript/core"
import { AccelerometerConfig } from "@devicescript/servers"
import { startSimulatorServer } from "./servers"

// shake/gesture detection based on
// https://github.com/lancaster-university/codal-core/blob/master/source/driver-models/Accelerometer.cpp
Expand Down Expand Up @@ -318,8 +319,40 @@ export async function startAccelerometer(
driver: AccelerometerDriver,
options: AccelerometerOptions & SensorServerOptions
): Promise<ds.Accelerometer> {
const server = new AccelerometerServer(driver, options)
const client = new ds.Accelerometer(startServer(server, options))
let roleName: string
if (ds.isSimulator() && !options.simOk)
roleName = startSimulatorServer({
name: options.roleName,
variant: () => options.variant,
spec: ds.Accelerometer.spec,
})
else {
const server = new AccelerometerServer(driver, options)
roleName = startServer(server, options)
}
const client = new ds.Accelerometer(roleName)
return client
}

/**
* Start an accelerometer. See also startIMU().
*/
export async function startGyroscope(
driver: GyroscopeDriver,
options: GyroscopeOptions & SensorServerOptions
): Promise<ds.Gyroscope> {
let roleName: string
if (ds.isSimulator() && !options.simOk)
roleName = startSimulatorServer({
name: options.roleName,
variant: () => options.variant,
spec: ds.Gyroscope.spec,
})
else {
const server = new GyroscopeServer(driver, options)
roleName = startServer(server, options)
}
const client = new ds.Gyroscope(roleName)
return client
}

Expand All @@ -330,11 +363,8 @@ export async function startIMU(
driver: AccelerometerDriver & GyroscopeDriver,
options: AccelerometerOptions & GyroscopeOptions & SensorServerOptions
) {
const accelerometer = new ds.Accelerometer(
startServer(new AccelerometerServer(driver, options), options)
)
const gyroscope = new ds.Accelerometer(
startServer(new GyroscopeServer(driver, options), options)
)
const accelerometer = startAccelerometer(driver, options)
const gyroscope = startGyroscope(driver, options)

return { accelerometer, gyroscope }
}
65 changes: 38 additions & 27 deletions packages/drivers/src/servers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export interface SimpleSensorBaseOptions {
minReading?(): ds.AsyncValue<number>
maxReading?(): ds.AsyncValue<number>
readingError?(): ds.AsyncValue<number>
variant?(): () => number
variant?: () => number
errorFraction?: number
errorValue?: number
min?: number
Expand All @@ -56,43 +56,54 @@ export interface SimpleSensorOptions extends SimpleSensorBaseOptions {
spec: ds.ServiceSpec
}

let simRoles: any

let _simRoles: any
function simRole(pref: string) {
for (let i = 0; ; i++) {
const k = pref + "_" + i
if (!simRoles[k]) {
simRoles[k] = true
if (!_simRoles[k]) {
_simRoles[k] = true
return k
}
}
}

export interface SimulatorServerOptions {
errorValue?: number
min?: number
max?: number

baseName?: string
name?: string
variant?: () => number
spec: ds.ServiceSpec
}

export function startSimulatorServer(options: SimulatorServerOptions) {
if (!_simRoles) _simRoles = {}
let name = options.name
if (!name) {
if (options.baseName) name = options.baseName + "_" + options.spec.name
else name = simRole(options.spec.name)
} else _simRoles[name] = true
const keys: (keyof SimulatorServerOptions)[] = ["min", "max", "errorValue"]
let num = 0
for (const key of keys) {
if (options[key] !== undefined) addkv(key, options[key])
}
if (options.variant) addkv("variant", options.variant())
console.debug(`request sim: ${name}`)
return name

function addkv(key: string, value: any) {
name += (num === 0 ? "?" : "&") + key + "=" + value
num++
}
}

export function startSimpleServer(
options: SimpleSensorOptions & ServerOptions
) {
if (ds.isSimulator() && !options.simOk) {
if (!simRoles) simRoles = {}
let name = options.name
if (!name) {
if (options.baseName)
name = options.baseName + "_" + options.spec.name
else name = simRole(options.spec.name)
} else simRoles[name] = true
const keys: (keyof SimpleSensorOptions)[] = ["min", "max", "errorValue"]
let num = 0
for (const key of keys) {
if (options[key] !== undefined) addkv(key, options[key])
}
if (options.variant) addkv("variant", options.variant())
console.debug(`request sim: ${name}`)
return name

function addkv(key: string, value: any) {
name += (num === 0 ? "?" : "&") + key + "=" + value
num++
}
}
if (ds.isSimulator() && !options.simOk) return startSimulatorServer(options)
return startServer(new SimpleSensorServer(options), options)
}

Expand Down
1 change: 1 addition & 0 deletions packages/server/src/servercore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type DsServer = typeof ds & {

export interface ServerOptions {
roleName?: string
simOk?: boolean
}

let eventWorker: SequentialWorker
Expand Down

0 comments on commit 7129d81

Please sign in to comment.