View unanswered posts | View active topics It is currently Fri Nov 28, 2014 7:19 am






Reply to topic  [ 6 posts ] 
Code not working 
Author Message
Rookie

Joined: Mon Nov 17, 2008 11:32 pm
Posts: 5
Post Code not working
Code:
#pragma config(Hubs,  S1, HTMotor,  HTServo,  none,     none)
#pragma config(Motor,  mtr_S1_C1_1,     motorD,        tmotorNone, openLoop)
#pragma config(Motor,  mtr_S1_C1_2,     motorE,        tmotorNone, openLoop)
#pragma config(Servo,  servo1,          s1,                   tServoNormal)
#pragma config(Servo,  servo2,          s2,                   tServoNormal)
#pragma config(Servo,  servo3,          s3,                   tServoNormal)
#pragma config(Servo,  servo4,          s4,                   tServoNormal)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#include "JoystickDriver.c"

void up()
{
  if (joy1Btn(1) == 1)
    {
         servo[s2] = ServoValue(s2) + 3;
    }
}

  void down()
  {
    if (joy1Btn(2) == 1)
    {
       servo[s2] = ServoValue(s2) - 3;
    }

}
task main ()
{
  while (true)
  {
  servoChangeRate[s2] = 2;
     getJoystickSettings(joystick);
     up();
     down();
   }
}



When button 1 is depressed the servo value goes up. When button 2 is depressed the servo value does not go down. Can anyone help us?

Thanks,


bcorriguex


Tue Feb 03, 2009 8:08 pm
Profile
Novice

Joined: Fri Oct 24, 2008 8:58 am
Posts: 87
Post Re: Code not working
The short answer:

try just
Code:
if (joy1Btn(2))   /* was if (joy1Btn(2) == 1) */
    {
       servo[s2] = ServoValue(s2) - 3;
    }


The long answer:
Look here at the joy1Btn macro
Code:
#define joy1Btn(btn)                    (joystick.joy1_Buttons & (1 << (btn - 1)))


the 1 << (btn-1) is the same as saying 2^(btn-1) which for button 2 is 2^1 = 2.
the joystick.joy1_Buttons & (...) is a bitwise AND operation the result of which will be 2 if the button is pressed ... not 1 which your code was looking for.

your code works for button 1 because 2^0 = 1.

so don't check the button macros against 1 just check them non-zero or zero.


Wed Feb 04, 2009 11:31 am
Profile
Rookie

Joined: Mon Nov 17, 2008 11:32 pm
Posts: 5
Post Re: Code not working
ok i some what understand what you ar saying, so its btn no. ^1 if not what is the formula?
i decided to go with using the code joystick.joy1_Buttons == btn no. it is working for buttons 1 - 3 but not for button 4.

this is what my code is exactly.

#pragma config(Hubs, S1, HTMotor, HTServo, none, none)
#pragma config(Motor, mtr_S1_C1_1, motorD, tmotorNone, openLoop)
#pragma config(Motor, mtr_S1_C1_2, motorE, tmotorNone, openLoop)
#pragma config(Servo, servo1, s1, tServoNormal)
#pragma config(Servo, servo2, s2, tServoNormal)
#pragma config(Servo, servo3, s3, tServoNormal)
#pragma config(Servo, servo4, s4, tServoNormal)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

#include "JoystickDriver.c"

void servos()
{
getJoystickSettings(joystick);
if (joystick.joy1_Buttons == 1)
{
servo[s1] = ServoValue(s1) + 3;
servo[s2] = ServoValue(s2) - 3;
}
if (joystick.joy1_Buttons == 2)
{
servo[s1] = ServoValue(s1) - 3;
servo[s2] = ServoValue(s2) + 3;
}
if (joystick.joy1_Buttons == 3)
{
servo[s3] = ServoValue(s3) - 3;
servo[s4] = ServoValue(s4) + 3;
}
if (joystick.joy1_Buttons == 4)
{
servo[s3] = ServoValue(s3) + 3;
servo[s4] = ServoValue(s4) - 3;
}
}

task main ()
{
while (true)
{
servoChangeRate[s1] = 1;
servoChangeRate[s2] = 1;
servoChangeRate[s3] = 1;
servoChangeRate[s4] = 1;
getJoystickSettings(joystick);
servos();
}
}


Thu Feb 05, 2009 9:37 pm
Profile
Novice

Joined: Fri Oct 24, 2008 8:58 am
Posts: 87
Post Re: Code not working
Sorry, I think the long answer confused you more than it helped.

just try
Code:
if (joy1Btn(1))
{
  servo[s1] = ServoValue(s1) + 3;
  servo[s2] = ServoValue(s2) - 3;
}
if (joy1Btn(2))
{
  servo[s1] = ServoValue(s1) - 3;
  servo[s2] = ServoValue(s2) + 3;
}
if (joy1Btn(3))
{
  servo[s3] = ServoValue(s3) - 3;
  servo[s4] = ServoValue(s4) + 3;
}

if (joy1Btn(4))
{
  servo[s3] = ServoValue(s3) + 3;
  servo[s4] = ServoValue(s4) - 3;
}



or change yours to:
Code:
if (joystick.joy1_Buttons == 1)
{
servo[s1] = ServoValue(s1) + 3;
servo[s2] = ServoValue(s2) - 3;
}
if (joystick.joy1_Buttons == 2)
{
servo[s1] = ServoValue(s1) - 3;
servo[s2] = ServoValue(s2) + 3;
}
if (joystick.joy1_Buttons == 4)
{
servo[s3] = ServoValue(s3) - 3;
servo[s4] = ServoValue(s4) + 3;
}
if (joystick.joy1_Buttons == 8)
{
servo[s3] = ServoValue(s3) + 3;
servo[s4] = ServoValue(s4) - 3;
}



But the first version is alot better since the second version ONLY works when the button is the only thing being pressed. The first version allows other buttons to be pressed at the same time (ie, the front trigger buttons)


I don't use the default joystick code myself, but I'm pretty sure this is the way the joy1Btn macro is supposed to be used.


Fri Feb 06, 2009 5:32 pm
Profile
Novice

Joined: Tue Jan 13, 2009 5:22 pm
Posts: 72
Location: USA, Pennsylvania
Post Re: Code not working
Are going to try most of these codes and see if they work, probably it will happen on Tuesday.

_________________
We need a Linux Version!


Fri Feb 06, 2009 5:52 pm
Profile WWW
Rookie

Joined: Mon Nov 17, 2008 11:32 pm
Posts: 5
Post Re: Code not working
i tried it out and that works verry well!

thanks,
Brian


Sat Feb 07, 2009 4:09 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 6 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.