rxpoweredup
Version:
A Typescript RxJS-based library for controlling LEGO Powered UP hubs & peripherals.
51 lines (50 loc) • 4.62 kB
JavaScript
import { MOTOR_ACC_DEC_DEFAULT_PROFILE_ID, MOTOR_LIMITS, MotorServoEndState, MotorUseProfile, PortModeName, PortOperationCompletionInformation, WELL_KNOWN_PORT_MODE_IDS, } from '../../constants';
export class MotorsFeature {
messenger;
portOutputCommandOutboundMessageFactoryService;
config;
constructor(messenger, portOutputCommandOutboundMessageFactoryService, config) {
this.messenger = messenger;
this.portOutputCommandOutboundMessageFactoryService = portOutputCommandOutboundMessageFactoryService;
this.config = config;
}
setAccelerationTime(portId, timeMs) {
const message = this.portOutputCommandOutboundMessageFactoryService.setAccelerationTime(portId, timeMs, MOTOR_ACC_DEC_DEFAULT_PROFILE_ID);
return this.execute(message);
}
setDecelerationTime(portId, timeMs) {
const message = this.portOutputCommandOutboundMessageFactoryService.setDecelerationTime(portId, timeMs, MOTOR_ACC_DEC_DEFAULT_PROFILE_ID);
return this.execute(message);
}
startPower(portId, power, powerModeId, options) {
const message = this.portOutputCommandOutboundMessageFactoryService.startPower(portId, power, powerModeId, options?.bufferMode ?? this.config.defaultBufferMode, options?.noFeedback ? PortOperationCompletionInformation.noAction : PortOperationCompletionInformation.commandFeedback);
return this.execute(message);
}
startSpeed(portId, speed, options) {
const message = this.portOutputCommandOutboundMessageFactoryService.startSpeed(portId, speed, options?.power ?? MOTOR_LIMITS.maxPower, options?.useProfile ?? MotorUseProfile.dontUseProfiles, options?.bufferMode ?? this.config.defaultBufferMode, options?.noFeedback ? PortOperationCompletionInformation.noAction : PortOperationCompletionInformation.commandFeedback);
return this.execute(message);
}
setSpeedSynchronized(virtualPortId, speed1, speed2, options) {
const message = this.portOutputCommandOutboundMessageFactoryService.startRotationSynchronized(virtualPortId, speed1, speed2, options?.power ?? MOTOR_LIMITS.maxPower, options?.useProfile ?? MotorUseProfile.dontUseProfiles, options?.bufferMode ?? this.config.defaultBufferMode, options?.noFeedback ? PortOperationCompletionInformation.noAction : PortOperationCompletionInformation.commandFeedback);
return this.execute(message);
}
goToPosition(portId, absoluteDegree, options) {
const message = this.portOutputCommandOutboundMessageFactoryService.goToAbsolutePosition(portId, absoluteDegree, options?.speed ?? MOTOR_LIMITS.maxSpeed, options?.power ?? MOTOR_LIMITS.maxPower, options?.endState ?? MotorServoEndState.hold, options?.useProfile ?? MotorUseProfile.dontUseProfiles, options?.bufferMode ?? this.config.defaultBufferMode, options?.noFeedback ? PortOperationCompletionInformation.noAction : PortOperationCompletionInformation.commandFeedback);
return this.execute(message);
}
goToPositionSynchronized(virtualPortId, targetDegree1, targetDegree2, options) {
const message = this.portOutputCommandOutboundMessageFactoryService.goToAbsolutePositionSynchronized(virtualPortId, targetDegree1, targetDegree2, options?.speed ?? MOTOR_LIMITS.maxSpeed, options?.power ?? MOTOR_LIMITS.maxPower, options?.endState ?? MotorServoEndState.hold, options?.useProfile ?? MotorUseProfile.dontUseProfiles, options?.bufferMode ?? this.config.defaultBufferMode, options?.noFeedback ? PortOperationCompletionInformation.noAction : PortOperationCompletionInformation.commandFeedback);
return this.execute(message);
}
setZeroPositionRelativeToCurrentPosition(portId, offset, positionModeId = WELL_KNOWN_PORT_MODE_IDS.motor[PortModeName.position]) {
const message = this.portOutputCommandOutboundMessageFactoryService.presetEncoder(portId, -offset, positionModeId);
return this.execute(message);
}
rotateByDegree(portId, degree, options) {
const message = this.portOutputCommandOutboundMessageFactoryService.startSpeedForDegrees(portId, Math.abs(degree), Math.abs(options?.speed ?? MOTOR_LIMITS.maxSpeed) * Math.sign(degree), options?.power ?? MOTOR_LIMITS.maxPower, options?.endState ?? MotorServoEndState.brake, options?.useProfile ?? MotorUseProfile.dontUseProfiles, options?.bufferMode ?? this.config.defaultBufferMode, options?.noFeedback ? PortOperationCompletionInformation.noAction : PortOperationCompletionInformation.commandFeedback);
return this.execute(message);
}
execute(message) {
return this.messenger.sendPortOutputCommand(message);
}
}