You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You mention that it takes 1ms each time to return values.
I've modified your code (untested on r-pi) and places it in to a class to store the values of the nunchuck in between reads which are now executed by calling the update_all() method of the class.
Modified code below.
from __future__ importdivisionimportwiringpiimportfcntlimportosclassPiNunchuck:
def__init__(i2c_file):
self.i2c_file=i2c_fileself.i2c_fd=-1self.i2c_addr=0x52self.i2c_ioctl_num=0x703self.joy_x_center=128self.joy_y_center=128self.joy_x=0self.joy_y=0self.acc_x=0self.acc_y=0self.acc_z=0self.btn_z=0self.btn_c=0self.setup()
defsetup(self):
self.i2c_fd=os.open(self.i2c_file, os.O_RDWR)
ret=fcntl.ioctl(self.i2c_fd, self.i2c_ioctl_num, self.i2c_addr)
ret=os.write(self.i2c_fd, "\xf0\x55")
os.write(self.i2c_fd, "\xfb\x00")
self.calibrate_joystick()
defcalibrate_joystick(self):
self.update_all()
self.joy_x_center=self.joy_xself.joy_y_center=self.joy_ydefupdate_all(self):
os.write(self.i2c_fd, "\x00")
wiringpi.delay(1)
ret=bytearray(os.read(self.i2c_fd, 6))
self.joy_x=ret[0]
self.joy_y=ret[1]
self.acc_x= (ret[2] <<2) + (ret[5] >>2&0x03)
self.acc_y= (ret[3] <<2) + (ret[5] >>4&0x03)
self.acc_z= (ret[4] <<2) + (ret[5] >>6&0x03)
self.btn_z=ret[5] &0x01==0self.btn_c=ret[5] &0x02==0defread_joy(self):
returnself.joy_x,self.joy_ydefread_acc(self):
returnself.acc_x,self.acc_y,self.acc_z# This is fairly basic. In my tests, without clamping, x ranged from -9 to 10# and y ranged from -9 to 8, so clamping to [-8, +8] gives us equal ranges# withou much loss of precision. Dividing by 10 and truncating to int means# small fluctuations get ignored, which is probably good for most purposes.defread_joy_normalized(self):
x, y= (self.joy_x,self.joy_y)
norm_x=clamp(int((x-self.joy_x_center)/10), -8, 8)
norm_y=clamp(int((y-self.joy_y_center)/10), -8, 8)
returnnorm_x, norm_ydefclamp(self,val, minval, maxval):
returnmin(maxval, max(minval, val))
The text was updated successfully, but these errors were encountered:
Thanks for your interest! I've basically lost interest in this project. It's a good suggestion, and I may do something like this if I ever pick up the code again, but for now I'm letting it gather dust.
You mention that it takes 1ms each time to return values.
I've modified your code (untested on r-pi) and places it in to a class to store the values of the nunchuck in between reads which are now executed by calling the update_all() method of the class.
Modified code below.
The text was updated successfully, but these errors were encountered: