0

I've tried searching for anything similar about my issue on several websites, including this one, but none I've found so far are similar. After searching about the term 1.#QO, I found something about quiet NaN, but I'm new to C in general, so I don't really understand the issue .

I'm trying to take the x and y values of a joystick, and when then use a formula for distance to find the distance between the joystick's position, and the joystick's natural resting position (0,0).

If it matters, I'm trying to do this in RobotC.

#pragma config(Hubs,  S1, HTMotor,  none,     none,     none)
#pragma config(Sensor, S1,     ,               sensorI2CMuxController)
#pragma config(Motor,  mtr_S1_C1_1,     DriveMotor,    tmotorTetrix, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#include "JoystickDriver.c"                                                                                                                                     

int calculateDrivePower(int joyStickX, int joyStickY)
{
    if (joyStickX != 0 & joyStickY != 0)
    {
        int joyDistance = (sqrt(pow((joyStickX - 0),2)+ pow((-joyStickY - 0),2)));
        joyDistance = ((joyDistance/127)*100);
        return (joyDistance);
    }
    else
    {
        return 0;
    }
}

task main() 
{
    int previousJoySlope = 0;
    int TurnSpeed = 70;
    while (true)
    {
        getJoystickSettings(joystick);
        if (abs(joystick.joy1_x1) > 10 || abs(joystick.joy1_y1) > 10)
    {
        writeDebugStreamLine("Test successful.");
        motor[DriveMotor] = calculateDrivePower(joystick.joy1_x1,joystick.joy1_y1);
    }
    }
}

If anyone could provide any insight, that'd be fantastic, thanks.

  • This might help: http://stackoverflow.com/q/5939573/535275 – Scott Hunter Jul 06 '14 at 00:55
  • So in essence, the debugger is reading 1.#QO because the number coming out of the equation needs to be rounded? – user3808679 Jul 06 '14 at 01:01
  • 1
    You could simplify your whole `calculateDrivePower` function to just `return sqrt(joyStickX * joyStickX + joyStickY * joyStickY) / 127 * 100;`. I wouldn’t be entirely surprised if that solved your issue, too. – icktoofay Jul 06 '14 at 01:06
  • That seemed to work. Guess it was just something in the math, thanks! – user3808679 Jul 06 '14 at 01:14

1 Answers1

0
if (joyStickX != 0 & joyStickY != 0)
{
    int joyDistance = (sqrt(pow((joyStickX - 0),2)+ pow((-joyStickY - 0),2)));
    joyDistance = ((joyDistance/127)*100);
    return (joyDistance);
}

The first issues that appears is the conditional within the if statement. The you have a single & that most likely should be &&:

if (joyStickX != 0 && joyStickY != 0)

(note: likely should be is used above because you can provide a conditional using a logical & of the tests joystickx != 0, but in this case it provides the same result. In that case, I would suggest the more readable && be used)

The next part of the code is simply the vector distance between 0,0 and the present position of the joystick. Of the general form dist^2 = (x2-x1)^2 + (y2-y1)^2 in your case x1,y1 = 0,0. Taking the square root of both sides provides dist = sqrt((x2-x1)^2 + (y2-y1)^2), or in C notation:

dist = (sqrt(pow((joyStickX - 0),2)+ pow((-joyStickY - 0),2)));

Next you have a scaling applied of dist = dist * 100/127 which provides the final distance returned. Hopefully this will help you understand what the code is doing.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85