Skip to content

Commit 7ccb690

Browse files
committed
js: Finish servo and DC motor classes
1 parent c0dfc73 commit 7ccb690

File tree

2 files changed

+95
-50
lines changed

2 files changed

+95
-50
lines changed

js/export.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ module.exports.Device = Device;
1212
module.exports.Motor = Motor;
1313
module.exports.Sensor = Sensor;
1414
module.exports.I2CSensor = I2CSensor;
15-
module.exports.PowerSupply = PowerSupply;
15+
module.exports.PowerSupply = PowerSupply;
16+
module.exports.ServoMotor = ServoMotor;
17+
module.exports.DCMotor = DCMotor;

js/motor.ts

Lines changed: 92 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,22 @@
33
///<reference path="io.ts" />
44

55
class MotorBase extends Device {
6-
private port: string;
7-
private deviceDir = '/sys/class/tacho-motor/'; //Default motor type
6+
protected port: string;
7+
protected deviceDir = '/sys/class/tacho-motor/'; //Default motor type
88

9-
private _deviceIndex: number = -1;
9+
protected _deviceIndex: number = -1;
1010
get deviceIndex(): number {
1111
return this._deviceIndex;
1212
}
1313

14-
get portName(): string {
15-
return this.port;
16-
}
17-
1814
get motorBaseProperties(): any {
1915
return {
2016
portName: 'port_name',
2117
type: 'type'
2218
};
2319
}
2420

25-
constructor(port: string, type: string) {
21+
constructor(port: string, type?: string) {
2622
super();
2723

2824
this.port = port;
@@ -46,10 +42,10 @@ class MotorBase extends Device {
4642
(port == ports.OUTPUT_AUTO)
4743
|| (port == undefined)
4844
|| (portName === port)
49-
) && (
45+
) && (
5046
(type == undefined || type == '')
5147
|| motorType == type
52-
);
48+
);
5349

5450
if (satisfiesCondition) {
5551
this._deviceIndex = Number(file.substring('motor'.length));
@@ -107,7 +103,7 @@ class Motor extends MotorBase {
107103

108104
constructor(port: string, type: string) {
109105
this.deviceDir = '/sys/class/tacho-motor/';
110-
106+
111107
super(port, type);
112108
}
113109

@@ -289,75 +285,122 @@ class Motor extends MotorBase {
289285
//DC Motor
290286
class DCMotor extends MotorBase {
291287

292-
get motorProperties(): any {
293-
return {
294-
command: 'command',
295-
commands: 'commands',
296-
name: 'name',
297-
dutyCycle: 'duty_cycle',
298-
rampUpMs: 'ramp_up_ms',
299-
rampDownMs: 'ramp_down_ms',
300-
polarity: 'polarity'
301-
};
302-
}
303-
304288
constructor(port: string) {
305289
this.deviceDir = '/sys/class/dc-motor/';
306-
290+
307291
super(port);
308292
}
309293

310294
//PROPERTIES
311-
get dutyCycle(): number {
312-
return this.getNumber(this.motorProperties.dutyCycle);
295+
set command(value: string) {
296+
this.setString("command", value);
297+
}
298+
299+
get commands(): string[] {
300+
return this.getString("commands").split(' ');
313301
}
314302

303+
get dutyCycle(): number {
304+
return this.getNumber("duty_cycle");
305+
}
315306
set dutyCycle(value: number) {
316-
this.setNumber(this.motorProperties.dutyCycle, value);
307+
this.setNumber("duty_cycle", value);
317308
}
318309

310+
get typeName(): string {
311+
return this.getString("name");
312+
}
319313

320-
get command(): string {
321-
return this.getString(this.motorProperties.command);
314+
get portName(): string {
315+
return this.getString("port_name");
322316
}
323317

324-
set command(value: string) {
325-
this.setString(this.motorProperties.command, value);
318+
get rampDownMs(): number {
319+
return this.getNumber("ramp_down_ms");
320+
}
321+
set rampDownMs(value: number) {
322+
this.setNumber("ramp_down_ms", value);
326323
}
327324

325+
get rampUpMs(): number {
326+
return this.getNumber("ramp_up_ms");
327+
}
328+
set rampUpMs(value: number) {
329+
this.setNumber("ramp_up_ms", value);
330+
}
328331

329-
get commands(): string[] {
330-
return this.getString(this.motorProperties.commands).split(' ');
332+
get polarity(): string {
333+
return this.getString("polarity");
331334
}
332-
333-
334-
get typeName(): string {
335-
return this.getString(this.motorProperties.name);
335+
set polarity(value: string) {
336+
this.setString("polarity", value);
336337
}
337-
338-
get rampDownMs(): number {
339-
return this.getNumber(this.motorProperties.rampDownMs);
338+
}
339+
340+
//Servo Motor
341+
class ServoMotor extends MotorBase {
342+
343+
constructor(port: string) {
344+
this.deviceDir = '/sys/class/servo-motor/';
345+
346+
super(port);
340347
}
341348

342-
set rampDownMs(value: number) {
343-
this.setNumber(this.motorProperties.rampDownMs, value);
349+
//PROPERTIES
350+
get command(): string {
351+
return this.getString("command");
352+
}
353+
set command(value: string) {
354+
this.setString("command", value);
344355
}
345356

357+
get typeName(): string {
358+
return this.getString("name");
359+
}
346360

347-
get rampUpMs(): number {
348-
return this.getNumber(this.motorProperties.rampUpMs);
361+
get portName(): string {
362+
return this.getString("port_name");
349363
}
350364

351-
set rampUpMs(value: number) {
352-
this.setNumber(this.motorProperties.rampUpMs, value);
365+
get maxPulseMs(): number {
366+
return this.getNumber("max_pulse_ms");
367+
}
368+
set maxPulseMs(value: number) {
369+
this.setNumber("max_pulse_ms", value);
370+
}
371+
372+
get midPulseMs(): number {
373+
return this.getNumber("mid_pulse_ms");
374+
}
375+
set midPulseMs(value: number) {
376+
this.setNumber("mid_pulse_ms", value);
353377
}
354378

379+
get minPulseMs(): number {
380+
return this.getNumber("min_pulse_ms");
381+
}
382+
set minPulseMs(value: number) {
383+
this.setNumber("min_pulse_ms", value);
384+
}
355385

356386
get polarity(): string {
357-
return this.getString(this.motorProperties.polarity);
387+
return this.getString("polarity");
358388
}
359-
360389
set polarity(value: string) {
361-
this.setString(this.motorProperties.polarity, value);
390+
this.setString("polarity", value);
391+
}
392+
393+
get position(): number {
394+
return this.getNumber("position");
395+
}
396+
set position(value: number) {
397+
this.setNumber("position", value);
398+
}
399+
400+
get rate(): number {
401+
return this.getNumber("rate");
402+
}
403+
set rate(value: number) {
404+
this.setNumber("rate", value);
362405
}
363406
}

0 commit comments

Comments
 (0)