View unanswered posts | View active topics It is currently Mon Nov 24, 2014 5:42 pm






Reply to topic  [ 4 posts ] 
Servo Question 
Author Message
Novice

Joined: Mon Oct 18, 2010 9:31 pm
Posts: 86
Post Servo Question
Question. Right now, when I press a button to move a servo it goes to the set position, but when I let go of the buton, it goes back to the starting position. Is there any way that I can have the servo go to the set postion by hitting the button and releasing the button, and then hitting the same button again to bring it back the starting point?


Wed Oct 12, 2011 9:41 am
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: Servo Question
To answer your question, yes, you can do that. Why don't you post your code, and them we can modify it to do what you want.

_________________
sudo rm -rf /


Wed Oct 12, 2011 10:18 am
Profile
Novice

Joined: Mon Oct 18, 2010 9:31 pm
Posts: 86
Post Re: Servo Question
I want to work it with my left and right flaps.

Code:
#pragma config(Hubs,  S1, HTMotor,  HTMotor,  HTServo,  HTServo)
#pragma config(Sensor, S1,     ,                    sensorI2CMuxController)
#pragma config(Sensor, S2,     HTMAG,               sensorHiTechnicMagnetic)
#pragma config(Sensor, S3,     ,                    sensorLightActive)
#pragma config(Motor,  motorA,          motorA,        tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  motorB,          motorB,        tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  motorC,          motorC,        tmotorNormal, PIDControl, reversed, encoder)
#pragma config(Motor,  mtr_S1_C1_1,     Right,         tmotorNormal, openLoop)
#pragma config(Motor,  mtr_S1_C1_2,     Left,          tmotorNormal, openLoop, reversed)
#pragma config(Motor,  mtr_S1_C2_1,     Lift,          tmotorNormal, openLoop)
#pragma config(Motor,  mtr_S1_C2_2,     MotorG,          tmotorNormal, openLoop, reversed)
#pragma config(Servo,  srvo_S1_C3_1,    Mag1,                 tServoStandard)
#pragma config(Servo,  srvo_S1_C3_2,    lift2,                tServoStandard)
#pragma config(Servo,  srvo_S1_C3_3,    leftflap3,            tServoStandard)
#pragma config(Servo,  srvo_S1_C3_4,    rightflap4,           tServoStandard)
#pragma config(Servo,  srvo_S1_C3_5,    MagR5,                tServoStandard)
#pragma config(Servo,  srvo_S1_C3_6,    servo6,               tServoNone)
#pragma config(Servo,  srvo_S1_C4_1,    L7,               tServoStandard)
#pragma config(Servo,  srvo_S1_C4_2,    R8,               tServoStandard)
#pragma config(Servo,  srvo_S1_C4_3,    servo9,               tServoNone)
#pragma config(Servo,  srvo_S1_C4_4,    servo10,              tServoNone)
#pragma config(Servo,  srvo_S1_C4_5,    servo11,              tServoNone)
#pragma config(Servo,  srvo_S1_C4_6,    servo12,              tServoNone)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#include "JoystickDriver.c"  //Include file to "handle" the Bluetooth messages.

void initializeRobot()
{
  servoTarget[Mag1] = 127;
  servoTarget[lift2] = 0;
  servoTarget[leftflap3] = 0;
  servoTarget[rightflap4] = 0;
  servoTarget[L7] = 0;
  servoTarget[R8] = 0;
  servoTarget[MagR5] = 0;

  return;
}

void processMagnet()
{
    if(SensorValue[HTMAG] > 670) // Magnetic Ball
    {
      servoTarget[Mag1] = 230;
      wait1Msec(1000);
    }
    else if(SensorValue[HTMAG] < 660)// Magnetic Ball
    {
      servoTarget[Mag1] = 230;
      wait1Msec(1000);
    }
    else
    {
      servoTarget[Mag1] = 127;
    }
}

task main()
{
  initializeRobot();

  waitForStart();   // wait for start of tele-op phase

  while (true)
  {

    getJoystickSettings(joystick);
    processMagnet();
    motor[Left] = joystick.joy1_y1;
    motor[Right] = joystick.joy1_y2;
    motor[Lift] = joystick.joy2_y1;// Ball Lift
    motor[motorG] = joystick.joy2_y2;

    //******************Lift Lock (End Game)******************
   if (joy2Btn(3))
    {
    servoTarget[lift2] = 255;//Locked
    }
   else
    {
     servoTarget[lift2] = 0; //Opened
   }
    //******************Left Flap******************
   if (joy1Btn(5))
    {
    servoTarget[leftflap3] = 60;//Out
    }
   else
    {
     servoTarget[leftflap3] = 0; //closed
   }
   //******************Right Flap******************
   if (joy1Btn(6))
    {
    servoTarget[rightflap4] = 60;//Out
    }
   else
    {
     servoTarget[rightflap4] = 0; //closed
   }
    //******************Lift Left Servos******************
   if (joy2Btn(5))
    {
    servoTarget[L7] = 255;//Open
    }
   else
    {
     servoTarget[L7] = 10; //Closed
   }
   //******************Lift Right Servos******************
   if (joy2Btn(6))
    {
    servoTarget[R8] = 0;
    }
   else
    {
     servoTarget[R8] = 245;
   }


   //******************Magnetic Release******************
   if (joy2Btn(2))
    {
    servoTarget[MagR5] = 60;//Out
    }
   else
    {
     servoTarget[MagR5] = 0; //closed
   }
 }
}


Wed Oct 12, 2011 4:15 pm
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: Servo Question
Alright, there are two ways to do this: multitasking, and boolean flags. Multitasking might be slightly simpler to write, but it is not really needed in this case, so I will demonstrate using boolean flags.
Code:
bool btn5Pressed = false;
bool leftFlapIsUp = false;
while(true){
//code
  if(!btn5Pressed && joy1Btn(5))
  {
    leftFlapIsUp = !leftFlapIsUp;
    btn5Pressed = true;
  }
  else if(!joy1Btn(5))
  {
    btn5Pressed = false;
  }
  if(leftFlapIsUp){
    servoTarget[leftflap3] = 60;
  }
  else
  {
    servoTarget[leftflap3] = 0;
  }
//code
}


There is the basic logic. The boolean variable leftFlapIsUp switches between true and false every time you press the button. The boolean variable btn5Pressed makes sure that a press is not recorded before you release the button and press it again. Some of the syntax might be off since I don't have ROBOTC in front of me, but you get the gist of the logic.

_________________
sudo rm -rf /


Wed Oct 12, 2011 4:47 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 4 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.