View unanswered posts | View active topics It is currently Mon Sep 15, 2014 8:56 pm






Reply to topic  [ 6 posts ] 
Servo Variable Issue 
Author Message
Rookie

Joined: Thu Mar 01, 2012 9:11 pm
Posts: 6
Post Servo Variable Issue
We built a robot to test a team member's idea of using a steering column type thing to drive. I created a variable for servo position and pushing the buttons should increase or decrease it by a small amount causing the steering column to turn. I have the brick set up to read out the values for the variable servopos, but whenever one of the buttons is pressed the variable jumps from it's initial 130 to 500 or even several thousand very quickly and I have no idea why.
Code:
#pragma config(Hubs,  S1, HTServo,  HTMotor,  none,     none)
#pragma config(Sensor, S2,     Sonar,               sensorSONAR)
#pragma config(Motor,  mtr_S1_C2_1,     drive,         tmotorNormal, openLoop)
#pragma config(Motor,  mtr_S1_C2_2,     motorE,        tmotorNormal, openLoop)
#pragma config(Servo,  srvo_S1_C1_1,    turn,                 tServoStandard)
#pragma config(Servo,  srvo_S1_C1_2,    servo2,               tServoNone)
#pragma config(Servo,  srvo_S1_C1_3,    servo3,               tServoNone)
#pragma config(Servo,  srvo_S1_C1_4,    servo4,               tServoNone)
#pragma config(Servo,  srvo_S1_C1_5,    servo5,               tServoNone)
#pragma config(Servo,  srvo_S1_C1_6,    servo6,               tServoNone)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#include "JoystickDriver.c"


task main()
{
  servo[turn]=135;
    long servopos=135;
    servopos=servo[turn];
while(true)
  {
    getJoystickSettings(joystick);

    nxtDisplayTextLine(2, "%d", servopos);
    if(joystick.joy1_y1 > 5 || joystick.joy1_y1 < -5)
    {
      motor[drive] = joystick.joy1_y1;
    }
    else
    {
      motor[drive] = 0;
    }

    if(joy1Btn(5))
    {
    servopos-=5;
    servo[turn] = (servopos);
    }
    if(joy1Btn(6))
    {
    servopos+=5;
    servo[turn] = (servopos);
    }
    if(joy1Btn(4))
    {
    servo[turn]=130;
    servopos = 130;
    }
  }
  }


As a quick fix i did this

Code:
#pragma config(Hubs,  S1, HTServo,  HTMotor,  none,     none)
#pragma config(Sensor, S2,     Sonar,               sensorSONAR)
#pragma config(Motor,  mtr_S1_C2_1,     drive,         tmotorNormal, openLoop)
#pragma config(Motor,  mtr_S1_C2_2,     motorE,        tmotorNormal, openLoop)
#pragma config(Servo,  srvo_S1_C1_1,    turn,                 tServoStandard)
#pragma config(Servo,  srvo_S1_C1_2,    servo2,               tServoNone)
#pragma config(Servo,  srvo_S1_C1_3,    servo3,               tServoNone)
#pragma config(Servo,  srvo_S1_C1_4,    servo4,               tServoNone)
#pragma config(Servo,  srvo_S1_C1_5,    servo5,               tServoNone)
#pragma config(Servo,  srvo_S1_C1_6,    servo6,               tServoNone)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#include "JoystickDriver.c"


task main()
{
  servo[turn]=135;
    long servopos=135;
    servopos=servo[turn];
while(true)
  {
    getJoystickSettings(joystick);

    nxtDisplayTextLine(2, "%d", servopos);
    if(joystick.joy1_y1 > 5 || joystick.joy1_y1 < -5)
    {
      motor[drive] = joystick.joy1_y1;
    }
    else
    {
      motor[drive] = 0;
    }

    if(joy1Btn(5))
    {
    servopos-=5;
    servo[turn] = (servopos/25);
    }
    if(joy1Btn(6))
    {
    servopos+=5;
    servo[turn] = (servopos/25);
    }
    if(joy1Btn(4))
    {
    servo[turn]=130;
    servopos = 130;
    }
  }
  }

I divide servopos by 25 to keep it working properly but this doesn't solve the problem. Any ideas on what went wrong or a more permanent solution?


Mon Sep 10, 2012 8:15 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Servo Variable Issue
Remember one thing, the NXT brick can run code really fast, especially since you do not have any wait1Msec() statement in your while (true) loop. Therefore, even if you just momentarily press button 5, for example, your robot loop probably ran 10's or 100's of times before your finger let go of the button which means your servopos variable will be incremented 10's or 100's of times. What you really want to do is not to check whether the button is pressed or not but to detect the transition of the button from not pressed to pressed.
Code:
bool btn5Pressed = false;
if (!btn5Pressed && joy1Btn(5)) {
    btn5Pressed = true;
    servopos -= 5;
    servo[turn] = servopos;
} else if (btn5Pressed && !joy1Btn(5)) {
    btn5Pressed = false;
}


Mon Sep 10, 2012 9:05 pm
Profile
Rookie

Joined: Mon Oct 22, 2012 8:57 am
Posts: 1
Post Re: Servo Variable Issue
Help what is wrong! thank you

pragma config(Hubs, S1, HTMotor, HTMotor, HTServo, none)
#pragma config(Sensor, S1, , sensorI2CMuxController)
#pragma config(Motor, mtr_S1_C1_2, Arm, tmotorTetrix, openLoop, reversed)
#pragma config(Motor, mtr_S1_C2_1, Drive1, tmotorTetrix, openLoop)
#pragma config(Motor, mtr_S1_C2_2, Drive2, tmotorTetrix, openLoop, reversed)
#pragma config(Servo, srvo_S1_C3_2, Grab, tServoTetrix)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

// ----------------------------------------------------------------------------------------------
#include "JoystickDriver.c"
// ----------------------------------------------------------------------------------------------
void initializeRobot()
{
// Place code here to initialize servos to starting positions.
servo[Grab] = 256;
return;
}
// ----------------------------------------------------------------------------------------------
task main() {
initializeRobot();
waitForStart();
int threshold = 25;
while (true) {
getJoystickSettings(joystick);
// ----------------------------------------------------------------------------------------------
// Driving Motors Coded Below
// ----------------------------------------------------------------------------------------------
if(abs(joystick.joy1_y2) > threshold)
{
if (joystick.joy1_y2 > 0) { motor[Drive1] = joystick.joy1_y2/-1; } // movement amount
if (joystick.joy1_y2 < 0) { motor[Drive1] = joystick.joy1_y2/-1; } // movement amount
}
else
{
motor[Drive1] = 0; // if no joystick movement then stop motor
}
// ---------------------------------------------------------------------------------------------
if(abs(joystick.joy1_y1) > threshold) // if the controller # is greater than threshold amount
{
if (joystick.joy1_y1 > 0) { motor[Drive2] = joystick.joy1_y1/-1; } // movement amount
if (joystick.joy1_y1 < 0) { motor[Drive2] = joystick.joy1_y1/-1; } // movement amount
}
else
{
motor[Drive2] = 0; // if no joystick movement then stop motor
}
// ----------------------------------------------------------------------------------------------
// ARM CODED BELOW
// ----------------------------------------------------------------------------------------------
if(abs(joystick.joy2_y1) > threshold)
{
motor[Arm] = joystick.joy2_y1/-1;
}
else
{
motor[Arm] = 0;
}
// ----------------------------------------------------------------------------------------------
// for SERVO REFERENCE on CONTINIOUS rotation...
// value = 0 is FULL POWER REVERSE
// value = 127 is STOP
// value = 256 id FULL POWER FORWARD
// ------------------ SERVO CODED BELOW ---------------------------------------------------------
if(joy2Btn(1) == 1)
{
servo[Grab] = 0;
}
if(joy2Btn(3) == 1)
{
servo[Grab] = 256;
}
if(joy2Btn(4) == 1)
{
servo[Grab] = 127;
}
// ----------------------------------------------------------------------------------------------
}
}


Mon Oct 22, 2012 4:17 pm
Profile
Site Admin
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 574
Post Re: Servo Variable Issue
The code compiled without errors for me; what specifically is the robot doing (or not doing) that is unexpected behavior?

On a side note, please create a new thread for new issues and only post on an existing thread if it directly relates to that thread's topic; this way, the clutter is kept to a minimum and it is easier for us to track and respond to questions in the forum.

_________________
Check out our Blog! And our Facebook page!
Need help? Take a look at our Wiki and our Forums.

I just met you,
And this is crazy,
But here's my code now,
So fix it, maybe?
~ Carly Rae Jepsen parody


Mon Oct 22, 2012 4:30 pm
Profile
Expert

Joined: Mon Oct 27, 2008 9:59 pm
Posts: 137
Post Re: Servo Variable Issue
In addition to John's comments about starting a new thread & giving more information about your issue, you should also consider including example code inside of the forum's Code tags as it will preserve your formatting as I've done below.

As to your possible issues; I see several questionable things just taking a quick look at your code even without you describing what isn't working. Below I have added a few comments asking for clarification where you may have bugs.
team4022 wrote:
Help what is wrong! thank you

Code:
pragma config(Hubs,  S1, HTMotor,  HTMotor,  HTServo,     none)
#pragma config(Sensor, S1,     ,               sensorI2CMuxController)
#pragma config(Motor,  mtr_S1_C1_2,     Arm,           tmotorTetrix, openLoop, reversed)
#pragma config(Motor,  mtr_S1_C2_1,     Drive1,        tmotorTetrix, openLoop)
#pragma config(Motor,  mtr_S1_C2_2,     Drive2,        tmotorTetrix, openLoop, reversed)
#pragma config(Servo,  srvo_S1_C3_2,    Grab, tServoTetrix)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

// ----------------------------------------------------------------------------------------------
#include "JoystickDriver.c"
// ----------------------------------------------------------------------------------------------
void initializeRobot()
{
  // Place code here to initialize servos to starting positions.
servo[Grab] = 256; //l0jec - Do you want the CR servo to start spinning before the start of the match?  Should this be set to 127?
return;
}
// ----------------------------------------------------------------------------------------------
task main() {
  initializeRobot();
  waitForStart();
  int threshold = 25;  //l0jec - 25 is large threshold; make sure you're moving the analog stick far enough to exceed it or consider a lower value
  while (true) {
  getJoystickSettings(joystick);
// ----------------------------------------------------------------------------------------------
// Driving Motors Coded Below
// ----------------------------------------------------------------------------------------------
if(abs(joystick.joy1_y2) > threshold) 
    {
      if (joystick.joy1_y2 > 0) { motor[Drive1] = joystick.joy1_y2/-1; } // movement amount  //l0jec - You already checked the value against your threshold up above.  Why are you checking to see if the value is greater than zero?  Also, why are you dividing the value by -1??
      if (joystick.joy1_y2 < 0) { motor[Drive1] = joystick.joy1_y2/-1; } // movement amount  //l0jec - You already checked the value against your threshold up above.  Why are you checking to see if the value is less than zero?  Also, why are you dividing the value by -1??
    else
    {
      motor[Drive1] = 0; // if no joystick movement then stop motor
    }
// ---------------------------------------------------------------------------------------------
if(abs(joystick.joy1_y1) > threshold) // if the controller # is greater than threshold amount
    {
      if (joystick.joy1_y1 > 0) { motor[Drive2] = joystick.joy1_y1/-1; } // movement amount  //l0jec - You already checked the value against your threshold up above.  Why are you checking to see if the value is greater than zero?  Also, why are you dividing the value by -1??
      if (joystick.joy1_y1 < 0) { motor[Drive2] = joystick.joy1_y1/-1; } // movement amount  //l0jec - You already checked the value against your threshold up above.  Why are you checking to see if the value is less than zero?  Also, why are you dividing the value by -1??
    }
      else
    {
        motor[Drive2] = 0; // if no joystick movement then stop motor
    }
// ----------------------------------------------------------------------------------------------
// ARM CODED BELOW
// ----------------------------------------------------------------------------------------------
if(abs(joystick.joy2_y1) > threshold)
    {
      motor[Arm] = joystick.joy2_y1/-1;  //l0jec - Again, why are you dividing the value by -1??
    }
    else
    {
      motor[Arm] = 0;
    }
// ----------------------------------------------------------------------------------------------
// for SERVO REFERENCE on CONTINIOUS rotation...
// value = 0 is FULL POWER REVERSE
// value = 127 is STOP
// value = 256 id FULL POWER FORWARD
// ------------------ SERVO CODED BELOW ---------------------------------------------------------
if(joy2Btn(1) == 1)
   {
      servo[Grab] = 0;
   }
if(joy2Btn(3) == 1)
   {
      servo[Grab] = 256;
   }
if(joy2Btn(4) == 1)
   {
      servo[Grab] = 127;
  }
// ----------------------------------------------------------------------------------------------
    }
}


Wed Oct 24, 2012 10:55 am
Profile
Expert

Joined: Mon Oct 27, 2008 9:59 pm
Posts: 137
Post Re: Servo Variable Issue
l0jec wrote:
In addition to John's comments about starting a new thread & giving more information about your issue, you should also consider including example code inside of the forum's Code tags as it will preserve your formatting as I've done below.

As to your possible issues; I see several questionable things just taking a quick look at your code even without you describing what isn't working. Below I have added a few comments asking for clarification where you may have bugs.
team4022 wrote:
Help what is wrong! thank you

Code:
pragma config(Hubs,  S1, HTMotor,  HTMotor,  HTServo,     none)
#pragma config(Sensor, S1,     ,               sensorI2CMuxController)
#pragma config(Motor,  mtr_S1_C1_2,     Arm,           tmotorTetrix, openLoop, reversed)
#pragma config(Motor,  mtr_S1_C2_1,     Drive1,        tmotorTetrix, openLoop)
#pragma config(Motor,  mtr_S1_C2_2,     Drive2,        tmotorTetrix, openLoop, reversed)
#pragma config(Servo,  srvo_S1_C3_2,    Grab, tServoTetrix)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

// ----------------------------------------------------------------------------------------------
#include "JoystickDriver.c"
// ----------------------------------------------------------------------------------------------
void initializeRobot()
{
  // Place code here to initialize servos to starting positions.
servo[Grab] = 256; //l0jec - Do you want the CR servo to start spinning before the start of the match?  Should this be set to 127?
return;
}
// ----------------------------------------------------------------------------------------------
task main() {
  initializeRobot();
  waitForStart();
  int threshold = 25;  //l0jec - 25 is a large threshold; make sure you're moving the analog stick far enough to exceed it or consider a lower value
  while (true) {
  getJoystickSettings(joystick);
// ----------------------------------------------------------------------------------------------
// Driving Motors Coded Below
// ----------------------------------------------------------------------------------------------
if(abs(joystick.joy1_y2) > threshold) 
    {
      if (joystick.joy1_y2 > 0) { motor[Drive1] = joystick.joy1_y2/-1; } // movement amount  //l0jec - You already checked the value against your threshold up above.  Why are you checking to see if the value is greater than zero?  Also, why are you dividing the value by -1??
      if (joystick.joy1_y2 < 0) { motor[Drive1] = joystick.joy1_y2/-1; } // movement amount  //l0jec - You already checked the value against your threshold up above.  Why are you checking to see if the value is less than zero?  Also, why are you dividing the value by -1??
    else
    {
      motor[Drive1] = 0; // if no joystick movement then stop motor
    }
// ---------------------------------------------------------------------------------------------
if(abs(joystick.joy1_y1) > threshold) // if the controller # is greater than threshold amount
    {
      if (joystick.joy1_y1 > 0) { motor[Drive2] = joystick.joy1_y1/-1; } // movement amount  //l0jec - You already checked the value against your threshold up above.  Why are you checking to see if the value is greater than zero?  Also, why are you dividing the value by -1??
      if (joystick.joy1_y1 < 0) { motor[Drive2] = joystick.joy1_y1/-1; } // movement amount  //l0jec - You already checked the value against your threshold up above.  Why are you checking to see if the value is less than zero?  Also, why are you dividing the value by -1??
    }
      else
    {
        motor[Drive2] = 0; // if no joystick movement then stop motor
    }
// ----------------------------------------------------------------------------------------------
// ARM CODED BELOW
// ----------------------------------------------------------------------------------------------
if(abs(joystick.joy2_y1) > threshold)
    {
      motor[Arm] = joystick.joy2_y1/-1;  //l0jec - Again, why are you dividing the value by -1??
    }
    else
    {
      motor[Arm] = 0;
    }
// ----------------------------------------------------------------------------------------------
// for SERVO REFERENCE on CONTINIOUS rotation...
// value = 0 is FULL POWER REVERSE
// value = 127 is STOP
// value = 256 id FULL POWER FORWARD
// ------------------ SERVO CODED BELOW ---------------------------------------------------------
if(joy2Btn(1) == 1)
   {
      servo[Grab] = 0;
   }
if(joy2Btn(3) == 1)
   {
      servo[Grab] = 256;
   }
if(joy2Btn(4) == 1)
   {
      servo[Grab] = 127;
  }
// ----------------------------------------------------------------------------------------------
    }
}


Wed Oct 24, 2012 10:58 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.