Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements on reading out all memory addresses of servos. #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 52 additions & 16 deletions dxl/dxlchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,20 +375,6 @@ def get_motor_list(self,broadcast=True,instantiate=True):
delay=time.time()-start
logging.debug("get_motor_list delay: %f"%delay)
return l

def get_configuration(self,broadcast=True):
"""Obtain the list of motors on a chain, read and return their full configuration"""
self.get_motor_list(broadcast=broadcast)
start=time.time()
d=OrderedDict()
for (id,m) in self.motors.items():
dd=OrderedDict()
d[id]=dd
for (name,r) in m.registers.items():
dd[name]=self.get_reg(id,name)
delay=time.time()-start
logging.debug("get_configuration reg delay: %f"%delay)
return d

def set_configuration(self,conf):
"""Compare the motors available on a chain to those specified in the provided configuration, silently try to set all registers, generates an exception if there is a mismatch"""
Expand Down Expand Up @@ -427,9 +413,59 @@ def set_configuration(self,conf):
self.set_reg(iid,name,val) # if writable set it

def dump(self):
"""Obtain the motors chain configuration and dumps it on stdout"""
conf=self.get_configuration()
print json.dumps(conf,indent=4,sort_keys=False)
dmp = json.dumps(conf,indent=4,sort_keys=False)
print dmp
return dmp

def get_configuration(self):
""" Gets the current configuration for all motors connected """
self.get_motor_list(broadcast=broadcast)

d = OrderedDict()

for (id, m) in self.motors.items():
dd = OrderedDict()
d[id] = dd

length, dump = self._get_configuration(m, id)

addr = 0
for value in dump:
name, length = m.getNameByAddr(addr)
if name is not None:
dd[name] = m.registers[name].fromdxl([dump[addr], dump[addr + 1]])
addr = addr + 1

return d

def _get_configuration(self, motor, id):
with self.lock:
""" Reads all memory addresses of a certain motor """
offset = 0x00
length = self._getRegSize(motor)

checksumed_data = [id, 4, 2, offset, length]

data = "".join(map(chr, [0xFF, 0xFF] + checksumed_data + [self.checksum(checksumed_data)]))
self.port.write(data)
self.port.flushOutput()

rec = self._recv()

return length, rec[1]

def _get_reg_size(self, motor):
""" Gets the register size of a certain motor type as defined in dxlmotors """

minaddr = 0x00
maxaddr = 0x00
for reg in motor.registers:
addr = motor.registers[reg].address + (motor.registers[reg].size - 1)
if maxaddr < addr:
maxaddr = addr

return maxaddr - minaddr

def get_motors(self,ids=None):
"""Return the list of all motors ids, or a specific set, or a single id"""
Expand Down
5 changes: 5 additions & 0 deletions dxl/dxlcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ def instantiateMotor(cls,id,model_number):
mcls=cls.DxlModels[model_number]
return mcls()

def getNameByAddr(self, addr):
for name in self.registers:
if self.registers[name].address == addr:
return name, self.registers[name].size

return None, None

def getRegisterCmd(self,name):
if not name in self.registers.keys():
Expand Down