ROBOTC.net forums
http://robotc.net/forums/

Joystick
http://robotc.net/forums/viewtopic.php?f=1&t=5179
Page 1 of 1

Author:  Alder [ Tue Nov 27, 2012 11:12 pm ]
Post subject:  Joystick

I made a program that was supposed to bind the motors to the joysticks, but when I run the program through the Field Control System, nothing happens. I am sure that it is connected to the robot, and I used code that was very close to the sample code. Anyone know what's wrong?
Here's the code:

Code:
#pragma config(Hubs,  S1, HTMotor,  HTServo,  none,     none)
#pragma config(Sensor, S1,     ,               sensorI2CMuxController)
#pragma config(Motor,  mtr_S1_C1_1,     motorD,        tmotorTetrix, openLoop)
#pragma config(Motor,  mtr_S1_C1_2,     motorE,        tmotorTetrix, openLoop)
#pragma config(Servo,  srvo_S1_C2_1,    servo1,               tServoNone)
#pragma config(Servo,  srvo_S1_C2_2,    servo2,               tServoNone)
#pragma config(Servo,  srvo_S1_C2_3,    servo3,               tServoNone)
#pragma config(Servo,  srvo_S1_C2_4,    servo4,               tServoNone)
#pragma config(Servo,  srvo_S1_C2_5,    servo5,               tServoNone)
#pragma config(Servo,  srvo_S1_C2_6,    servo6,               tServoNone)
#include "JoystickDriver.c"


task main()
{
   while(true)
      bMotorReflected[motorD] = true;
      bMotorReflected[motorE] = false;
{
    getJoystickSettings(joystick);
  motor[motorD] = joy1_y1;
  motor[motorE] = joy1_y2;
}
}

Author:  MHTS [ Wed Nov 28, 2012 2:04 am ]
Post subject:  Re: Joystick

Alder wrote:
I made a program that was supposed to bind the motors to the joysticks, but when I run the program through the Field Control System, nothing happens. I am sure that it is connected to the robot, and I used code that was very close to the sample code. Anyone know what's wrong?
Here's the code:

Code:
task main()
{
    while(true)
   bMotorReflected[motorD] = true;
    bMotorReflected[motorE] = false;
    {
         getJoystickSettings(joystick);
          motor[motorD] = joy1_y1;
          motor[motorE] = joy1_y2;
    }
}

The above code will loop forever setting bMotorReflected to true. I think what you really meant to do is:
Code:
    while(true)
    {
         getJoystickSettings(joystick);
          motor[motorD] = joystick.joy1_y1;
          motor[motorE] = joystick.joy1_y2;
    }

Author:  amcerbu [ Wed Nov 28, 2012 2:05 am ]
Post subject:  Re: Joystick

Alder wrote:
I made a program that was supposed to bind the motors to the joysticks, but when I run the program through the Field Control System, nothing happens. I am sure that it is connected to the robot, and I used code that was very close to the sample code. Anyone know what's wrong?
Here's the code:
(code)

You encountered a minor (and very easily made) logic error. If a while loop or an if clause is not immediately followed by a series of { curly braces }, it will execute the next command only. That allows for programming like...
Code:
if (bool1)
   function1();

... which is logically equivalent, but syntactically simpler than ...
Code:
if (bool2)
{
   function2();
}


On the other hand, this code is misleading:
Code:
if (false)
   function1(); // Won't be executed
   function2(); // Will be executed


In your code, the command bMotorReflected[motorD] = true; immediately follows while (true), but you don't supply braces. The program executes this line forever, since it doesn't know to include all the following code. You can fix the bug by moving the while (true) statement to the line just before the braces. This also saves redundant coding (and therefore improves performance marginally -- very marginally); your program won't be constantly making assignment calls to the bMotorReflected Boolean array.

Code:
task main()
{
   bMotorReflected[motorD] = true;
   bMotorReflected[motorE] = false;
   
   while (true)
   {
       getJoystickSettings(joystick);
       motor[motorD] = joy1_y1;
       motor[motorE] = joy1_y2;
   }
}


EDIT: Whoops, didn't see MHTS beat me to it. :)

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/