0

I'm having a little trouble using the accelerometer data using cwiid. I know there's already an answered question on this, but the solution wouldn't work, and because I can't comment, I have to make a new question. I'm using cwiid to make a turtle move in different directions, and everything else is fine, it's just this last stretch I can't figure out. It specifies the roll=(wm.state[etc." as the problem, but I put every other line concerning the accelerometer just in case. The report says roll=(wm.state['acc'][0]-125) KeyError: 'acc'

import cwiid, time, turtle, matplotlib

roll=(wm.state['acc'][0]-125) **# <--Trouble here**
pitch=(wm.state['acc'][1]-121)

try:
  wii=cwiid.Wiimote()
  time.sleep(3)
except:
  exit()

if (buttons & cwiid.BTN_B):
  if (roll < -5):
    turtle.seth(0)
    turtle.back(2)
  if (roll > 5):
    turtle.seth(0)
    turtle.forward(2)
  if (pitch > 5):
    turtle.seth(90)
    turtle.forward(2)
  if (pitch < -5):
    turtle.seth(90)
    turtle.back(2)
  time.sleep(button_delay)
  • You could try printing out `wm.state` just before the problem line, to see what keys it *does* actually have. – jasonharper Jan 31 '22 at 03:00
  • Can you please add a link to the other question you reference? It may help future searchers find the answer to their question. – ramzeek Feb 09 '22 at 04:28

1 Answers1

0

Ok, so it turns out I just forgot to type a single line that, from what I can tell, sets the state to accelerometer mode or something. I'm not too familiar with the cwiid library. Here's the required code to get the accelerometer data working for anyone who's having trouble and stumbles upon this.

    wii.rpt_mode = cwiid.RPT_BTN | cwiid.RPT_ACC
    roll=(wii.state['acc'][0])
    pitch=(wii.state['acc'][1])
    check = 0
    while check == 0:
      if (roll < threshold):
        #what you want it to do
      if (roll > threshold):
        #what you want it to do
      if (pitch < threshold):
        #what you want it to do
      if (pitch > threshold):
        #what you want it to do