View Single Post
  #2   Spotlight this post!  
Unread 27-03-2007, 17:39
Bongle's Avatar
Bongle Bongle is offline
Registered User
FRC #2702 (REBotics)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2002
Location: Waterloo
Posts: 1,069
Bongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond repute
Send a message via MSN to Bongle
Re: Joystick filtering gone horribly wrong...help!

Code:
} else if (temp_joystick < 0) {
		printf("lookup[temp_joystick] = %d, lookup[temp_joystick-1] = %d, output is %d\r\n", (int)lookup[temp_joystick], (int)lookup[temp_joystick-1], (int)((joystick - lookup[temp_joystick-1])*(lookup[temp_joystick] - lookup[temp_joystick-1]) / 21) + lookup[temp_joystick-1]);
		return ((joystick - lookup[temp_joystick-1])*(lookup[temp_joystick] - lookup[temp_joystick-1]) / 21) + lookup[temp_joystick-1];
On this here, you access negative members of an array. The only time you will ever enter this code is when temp_joystick < 0, and then you use it to address your lookuptable.

That's one of a few issues. I'm coding up an integer version right now.

You need to have a power of two number of line points. They are assumed to be evenly spaced between 0 and 255.
Code:
#define BITS_FOR_LOOKUP 3 // we're using the most significant 3 bits of the joystick value

#include <iostream>
using namespace std;

unsigned char lookup[8] = {0,31,63,95,  126,158,190,221};

unsigned char joyFilter(unsigned char joyValue)
{
  unsigned char n = joyValue>>(8-BITS_FOR_LOOKUP);

  // n is between 0 and 7 for sure
  unsigned int last;
  unsigned  int next;
  unsigned  int howFarAlong = joyValue - (n<<(8-BITS_FOR_LOOKUP)); // finds out how along joyValue is between n and n+1
  unsigned  int maxFarAlong = (1<<(8-BITS_FOR_LOOKUP));
  unsigned  int invHowFarAlong = (maxFarAlong - howFarAlong);


  if(n+1 >= 8)
  {
      last = lookup[7];
      next = 256;
  }
  else
  {
      last = lookup[n];
      next = lookup[n+1];
  }

  return (invHowFarAlong*last)/maxFarAlong + (howFarAlong*next)/maxFarAlong ;


}

Last edited by Bongle : 27-03-2007 at 18:04.