Skip to content

Commit e41619b

Browse files
author
marcusw
committed
Added two new HiTechnic sensors: ServoCon and MotorCon. These seem to be pretty rare and aren't listed on the HiTechnic website for some reason, but they do in fact exist.
1 parent d74cd58 commit e41619b

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed

nxt/sensor/hitechnic.py

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def set_mode(self, mode):
8282
Compass.add_compatible_sensor(None, 'HiTechnc', 'Compass ') #Tested with version '\xfdV1.23 '
8383
Compass.add_compatible_sensor(None, 'HITECHNC', 'Compass ') #Tested with version '\xfdV2.1 '
8484

85+
8586
class Accelerometer(BaseDigitalSensor):
8687
'Object for Accelerometer sensors. Thanks to Paulo Vieira.'
8788
I2C_ADDRESS = BaseDigitalSensor.I2C_ADDRESS.copy()
@@ -458,3 +459,153 @@ def set_digital_modes(self, modes):
458459
self.write_value('digital_cont', (int(modes), ))
459460

460461
Prototype.add_compatible_sensor(None, 'HiTechnc', 'Proto ')
462+
463+
464+
class ServoCon(BaseDigitalSensor):
465+
"""Object for HiTechnic FIRST Servo Controllers. Coded to HiTechnic's specs for
466+
the sensor but not tested. Please report whether this worked for you or not!"""
467+
I2C_ADDRESS = BaseDigitalSensor.I2C_ADDRESS.copy()
468+
I2C_ADDRESS.update({
469+
'status': (0x40, 'B'),
470+
'steptime': (0x41, 'B'),
471+
's1pos': (0x42, 'B'),
472+
's2pos': (0x43, 'B'),
473+
's3pos': (0x44, 'B'),
474+
'p4pos': (0x45, 'B'),
475+
'p5pos': (0x46, 'B'),
476+
'p6pos': (0x47, 'B'),
477+
'pwm': (0x46, 'B'),
478+
})
479+
480+
class Status:
481+
RUNNING = 0x00 #all motors stopped
482+
STOPPED = 0x01 #motor(s) moving
483+
484+
def __init__(self, brick, port, check_compatible=True):
485+
super(ServoCon, self).__init__(brick, port, check_compatible)
486+
487+
def get_status(self):
488+
"""Returns the status of the motors. 0 for all stopped, 1 for
489+
some running.
490+
"""
491+
return self.read_value('status')[0]
492+
493+
def set_step_time(self, time):
494+
"""Sets the step time (0-15).
495+
"""
496+
self.write_value('steptime', (time, ))
497+
498+
def set_pos(self, num, pos):
499+
"""Sets the position of a server. num is the servo number (1-6),
500+
pos is the position (0-255).
501+
"""
502+
self.write_value('s%dpos' % num, (pos, ))
503+
504+
def get_pwm(self):
505+
"""Gets the "PWM enable" value. The function of this value is
506+
nontrivial and can be found in the documentation for the sensor.
507+
"""
508+
return self.read_value('pwm')[0]
509+
510+
def set_pwm(self, pwm):
511+
"""Sets the "PWM enable" value. The function of this value is
512+
nontrivial and can be found in the documentation for the sensor.
513+
"""
514+
self.write_value('pwm', (pwm, ))
515+
516+
ServoCon.add_compatible_sensor(None, 'HiTechnc', 'ServoCon')
517+
518+
519+
class MotorCon(BaseDigitalSensor):
520+
"""Object for HiTechnic FIRST Motor Controllers. Coded to HiTechnic's specs for
521+
the sensor but not tested. Please report whether this worked for you or not!"""
522+
I2C_ADDRESS = BaseDigitalSensor.I2C_ADDRESS.copy()
523+
I2C_ADDRESS.update({
524+
'm1enctarget': (0x40, '>l'),
525+
'm1mode': (0x44, 'B'),
526+
'm1power': (0x45, 'b'),
527+
'm2power': (0x46, 'b'),
528+
'm2mode': (0x47, 'B'),
529+
'm2enctarget': (0x48, '>l'),
530+
'm1enccurrent': (0x4c, '>l'),
531+
'm2enccurrent': (0x50, '>l'),
532+
'batteryvoltage': (0x54, '2B'),
533+
'm1gearratio': (0x56, 'b'),
534+
'm1pid': (0x57, '3B'),
535+
'm2gearratio': (0x5a, 'b'),
536+
'm2pid': (0x5b, '3B'),
537+
})
538+
539+
class PID_Data():
540+
def __init__(self, p, i, d):
541+
self.p, self.i, self.d = p, i, d
542+
543+
def __init__(self, brick, port, check_compatible=True):
544+
super(MotorCon, self).__init__(brick, port, check_compatible)
545+
546+
def set_enc_target(self, mot, val):
547+
"""Set the encoder target (-2147483648-2147483647) for a motor
548+
"""
549+
self.write_value('m%denctarget'%mot, (val, ))
550+
551+
def get_enc_target(self, mot):
552+
"""Get the encoder target for a motor
553+
"""
554+
return self.read_value('m%denctarget'%mot)[0]
555+
556+
def get_enc_current(self, mot):
557+
"""Get the current encoder value for a motor
558+
"""
559+
return self.read_value('m%denccurrent'%mot)[0]
560+
561+
def set_mode(self, mot, mode):
562+
"""Set the mode for a motor. This value is a bit mask and you can
563+
find details about it in the sensor's documentation.
564+
"""
565+
self.write_value('m%dmode'%mot, (mode, ))
566+
567+
def get_mode(self, mot):
568+
"""Get the mode for a motor. This value is a bit mask and you can
569+
find details about it in the sensor's documentation.
570+
"""
571+
return self.read_value('m%dmode'%mot)[0]
572+
573+
def set_power(self, mot, power):
574+
"""Set the power (-100-100) for a motor
575+
"""
576+
self.write_value('m%dpower'%mot, (power, ))
577+
578+
def get_power(self, mot):
579+
"""Get the power for a motor
580+
"""
581+
return self.read_value('m%dpower'%mot)[0]
582+
583+
def set_gear_ratio(self, mot, ratio):
584+
"""Set the gear ratio for a motor
585+
"""
586+
self.write_value('m%dgearratio'%mot, (ratio, ))
587+
588+
def get_gear_ratio(self, mot):
589+
"""Get the gear ratio for a motor
590+
"""
591+
return self.read_value('m%dgearratio'%mot)[0]
592+
593+
def set_pid(self, mot, piddata):
594+
"""Set the PID coefficients for a motor. Takes data in
595+
MotorCon.PID_Data(p, i, d) format.
596+
"""
597+
self.write_value('m%dpid'%mot, (piddata.p, piddata.i, piddata.d))
598+
599+
def get_pid(self, mot):
600+
"""Get the PID coefficients for a motor. Returns a PID_Data() object.
601+
"""
602+
p, i, d = self.read_value('m%dpid'%mot)
603+
return self.PID_Data(p, i, d)
604+
605+
def get_battery_voltage(self):
606+
"""Gets the battery voltage (in millivolts/20)
607+
"""
608+
high, low = self.read_value('bateryvoltage')[0]
609+
return high << 2 + low
610+
611+
MotorCon.add_compatible_sensor(None, 'HiTechnc', 'MotorCon')

0 commit comments

Comments
 (0)