View unanswered posts | View active topics It is currently Fri Oct 24, 2014 8:43 pm






Reply to topic  [ 3 posts ] 
Joystick 
Author Message
Rookie
User avatar

Joined: Thu Oct 04, 2012 8:51 pm
Posts: 22
Post 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;
}
}


Tue Nov 27, 2012 11:12 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post 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;
    }


Wed Nov 28, 2012 2:04 am
Profile
Novice

Joined: Sun Oct 21, 2012 10:01 pm
Posts: 76
Post 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. :)


Wed Nov 28, 2012 2:05 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.