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

programming servos in robot c
http://www.robotc.net/forums/viewtopic.php?f=52&t=1235
Page 1 of 1

Author:  Rhys Jones [ Mon Feb 16, 2009 6:55 pm ]
Post subject:  programming servos in robot c

I am trying to insert servos into the teleop program, but they are not working. I am just trying to have the servos move to one position at the beginning at the teleop and then go back to where they were by pressing a controller button.

Or I could have the servos move at the end of the autonomous.

Any suggestions?

Author:  Emilhem [ Mon Feb 16, 2009 9:02 pm ]
Post subject:  Re: programming servos in robot c

I had a lot of problems with servos and submitted a topic about it.
http://robotc.net/forums/viewtopic.php?f=33&t=1189

Author:  Rhys Jones [ Mon Feb 16, 2009 10:27 pm ]
Post subject:  Re: programming servos in robot c

but how do you get the servos to work with one of the controller buttons?

Author:  Atlantisbase [ Tue Feb 17, 2009 12:05 am ]
Post subject:  Re: programming servos in robot c

True, you could have your servo move into position at the end of the Autonomus program but it would be better to place a command like this, if intended for the TeleOp portion, in the initializeRobot() function of the TeleOp program so that it is set at the beginning of the program. Then to move a servo when a button is pressed call either joy1Btn(btnNum) or joy2Btn(btnNum) as the condition for an if statement to test for the press of a button whose number is btnNum on either joystick 1 or 2 respectivly. Buttons 11 and 12, the "stick" buttons, are not used in RobotC.

Also as others have stated, follow these guidelines when testing for button presses:
Code:
//Do not use this form to test for a button press
//Form 1
if(joy1Btn(3) == 1) //This will return false because the function returns the bitmap value of the button which is not necessarily 1
---------------------------------------------------------------
//Better to use this
//Form 2
if(joy1Btn(3)) //Evaluates to true for any value greater than 0
---------------------------------------------------------------
//While you could use this form, it is better to use Form 2
//Form 3
if(joy1Btn(3) != 0) //Tests explictly for any value greater than 0 but could be any value other than 0
==================================================================================

//Also, to test for the absence of a button press you should not use this
//Form 1
if(joy1Btn(3) != 1) //This will evaluate to true if the button is pressed or not pressed
-----------------------------------------------------------------------------------
//This is best
//Form 2
if(!joy1Btn(3)) //Evaluates true for any value less than or equal to 0
-----------------------------------------------------------------------------------
//Again this will work but From 2 is best
//Form 3
if(joy1Btn(3) == 0) //Tests explictly for 0; may not always be true


Now, a full example.
Code:
#pragma config(Hubs,  S1, HTMotor,  HTMotor,  HTServo,  none) //Make sure you set up both the servo controller...
#pragma config(Servo,  srvo_S1_C3_1,    armServo,             tServoNormal)  //and servo in the TeleOp, just as was done in the Autonomus program from within the Motors and Sensors Setup dialog.

void initializeRobot()
{
    servo[armServo] = 250 //Insert your value here
}

task main()
{
    initializeRobot();

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

    while (true)
    {
        if(joy1Btn(1)) //Test for the press of button 1 on joystick 1. Insert appropriate button here.
        {
            servo[armServo] = 120; //Move the servo to the desired position. Insert your value here
        }
    }
}

Author:  Rhys Jones [ Tue Feb 17, 2009 6:34 pm ]
Post subject:  Re: programming servos in robot c

I redid the code using your information, however it didn't work and when I ran the debugger it stopped atthe second servo

which is servo[servo2] = 127;

Here is the whole code

#pragma config(Hubs, S1, HTMotor, HTMotor, none, none)
#pragma config(Motor, mtr_S1_C1_1, motorD, tmotorNormal, openLoop)
#pragma config(Motor, mtr_S1_C1_2, motorE, tmotorNormal, openLoop)
#pragma config(Motor, mtr_S1_C2_1, motorF, tmotorNormal, openLoop)
#pragma config(Motor, mtr_S1_C2_2, motorG, tmotorNormal, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

/////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tele-Operation Mode Code Template
//
// This file contains a template for simplified creation of an tele-op program for an FTC
// competition.
//
// You need to customize two functions with code unique to your specific robot.
//
/////////////////////////////////////////////////////////////////////////////////////////////////////

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


/////////////////////////////////////////////////////////////////////////////////////////////////////
//
// initializeRobot
//
// Prior to the start of tele-op mode, you may want to perform some initialization on your robot
// and the variables within your program.
//
// In most cases, you may not have to add any code to this function and it will remain "empty".
//
/////////////////////////////////////////////////////////////////////////////////////////////////////

void initializeRobot()
{
// Place code here to sinitialize servos to starting positions.
// Sensors are automatically configured and setup by ROBOTC. They may need a brief time to stabilize.
servo[servo1] = 127;
servo[servo2] = 127;
return;
}


/////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Main Task
//
// The following is the main code for the tele-op robot operation. Customize as appropriate for
// your specific robot.
//
// Game controller / joystick information is sent periodically (about every 50 milliseconds) from
// the FMS (Field Management System) to the robot. Most tele-op programs will follow the following
// logic:
// 1. Loop forever repeating the following actions:
// 2. Get the latest game controller / joystick settings that have been received from the PC.
// 3. Perform appropriate actions based on the joystick + buttons settings. This is usually a
// simple action:
// * Joystick values are usually directly translated into power levels for a motor or
// position of a servo.
// * Buttons are usually used to start/stop a motor or cause a servo to move to a specific
// position.
// 4. Repeat the loop.
//
// Your program needs to continuously loop because you need to continuously respond to changes in
// the game controller settings.
//
// At the end of the tele-op period, the FMS will autonmatically abort (stop) execution of the program.
//
/////////////////////////////////////////////////////////////////////////////////////////////////////

task main()
{
initializeRobot();

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

bMotorReflected[motorD]=true;
bMotorReflected[motorE]=false;

bMotorReflected[motorF]=true;

while (true)
{
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
//// ////
//// Add your robot specific tele-op code here. ////
//// ////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////

// Insert code to have servos and motors respond to joystick and button values.
getJoystickSettings(joystick);
motor[motorD] = joystick.joy1_y1;
motor[motorE] = joystick.joy1_y2;

motor[motorG] = 0.75*joystick.joy2_y1;
motor[motorF] = 0.75*joystick.joy2_y2;

if(joy2Btn(07))
{
servo[servo1] = 0;
}
if(joy2Btn(07))
{
servo[servo2] = 0;
}
// Look in the ROBOTC samples folder for programs that may be similar to what you want to perform.
// You may be able to find "snippets" of code that are similar to the functions that you want to
// perform.
}
}

Author:  Emilhem [ Tue Feb 17, 2009 8:04 pm ]
Post subject:  Re: programming servos in robot c

To Rhys Jones.

You don't have a "void waitForStart" any where, I can't see any other problems.

Author:  10nisman94 [ Tue Feb 17, 2009 8:09 pm ]
Post subject:  Re: programming servos in robot c

the viod wait for start is in the new Joystick Driver.c that was released last month.

Author:  Atlantisbase [ Wed Feb 18, 2009 12:01 am ]
Post subject:  Re: programming servos in robot c

First of all, make sure you have set up your servo controller and servos from the Motors and Sensors Setup dialog, the appropriate declarations are missing from the beginning of your code. I'm suprised it let you compile the code at all considering servo1 was not even declared. As others have pointed out, make sure you are using the templates and updated "JoystickDriver.c"

Additionally, there is no need to test for the press of a button twice as you have,
Code:
if(joy2Btn(07))
{
    servo[servo1] = 0;
}
if(joy2Btn(07))
{
    servo[servo2] = 0;
}

These two tests can be combined into one:
Code:
if(joy2Btn(7))
{
    servo[servo1] = 0;
    servo[servo2] = 0;
}

And there is also no need to test for "07", it may get confused, though it's doubtful.

Author:  Rhys Jones [ Wed Feb 18, 2009 1:01 pm ]
Post subject:  Re: programming servos in robot c

Thanks for the input. I tried different things but I still can't get the servos to respond. Below is my lasted attempt. Any new suggestions?


// Insert code to have servos and motors respond to joystick and button values.
getJoystickSettings(joystick);
motor[motorD] = joystick.joy1_y1;
motor[motorE] = joystick.joy1_y2;

motor[motorG] = 0.75*joystick.joy2_y1;
motor[motorF] = 0.75*joystick.joy2_y2;

if(joy2Btn(07))
{
servo[motorH] = 127;
servo[motorI] = 127;
}
// Look in the ROBOTC samples folder for programs that may be similar to what you want to perform.
// You may be able to find "snippets" of code that are similar to the functions that you want to
// perform.
}
}

Author:  Atlantisbase [ Wed Feb 18, 2009 3:33 pm ]
Post subject:  Re: programming servos in robot c

No definitely not.
Code:
servo[motorH] = 127;
servo[motorI] = 127;

This is most definitely wrong. Are you sure you have the servos set up correctly?

Author:  Emilhem [ Wed Feb 18, 2009 6:23 pm ]
Post subject:  Re: programming servos in robot c

Try to turn the connection to the servos 180*, it's hard to recognize the differences in the beginning but it is probably a wiring problem.

Author:  Rhys Jones [ Wed Feb 18, 2009 7:45 pm ]
Post subject:  Re: programming servos in robot c

Atlantisbase wrote:
No definitely not.
Code:
servo[motorH] = 127;
servo[motorI] = 127;

This is most definitely wrong. Are you sure you have the servos set up correctly?


When I attempt to set up the servos, it returns an error upon compiling. This didn't happen to me originally, but now it seems to happen in whatever program I create.

#pragma config(Hubs, S1, HTMotor, HTMotor, HTServo, none)
#pragma config(Motor, mtr_S1_C1_1, motorD, tmotorNormal, openLoop)
#pragma config(Motor, mtr_S1_C1_2, motorE, tmotorNormal, openLoop)
#pragma config(Motor, mtr_S1_C2_1, motorF, tmotorNormal, openLoop)
#pragma config(Motor, mtr_S1_C2_2, motorG, tmotorNormal, openLoop)
#pragma config(Servo, srvo_S1_C3_1, servo1, tServoNormal)
#pragma config(Servo, srvo_S1_C3_2, servo2, tServoNormal)

The bottom lines both have the errors:
**Error**:Variable 'servo1' redefinition.
**Error**:Variable 'servo2' redefinition.
When I named the servos motors H and I, it didn't have errors, which is why I attempted to rename the servos.

Author:  Atlantisbase [ Wed Feb 18, 2009 7:55 pm ]
Post subject:  Re: programming servos in robot c

When you named them "motorH" and "motorI" where these still servo declarations, i.e.
Code:
#pragma config(Servo, srvo_S1_C3_1, motorH, tServoNormal)

or were they motor declarations,
Code:
#pragma config(Motor, mtr_S1_C1_1, motorH, tmotorNormal, openLoop)

If the former is true then try naming them something other than "servo1" and "servo2" which are the internal name for those servos. The later simply won't work.

Author:  Rhys Jones [ Fri Feb 20, 2009 8:10 pm ]
Post subject:  Re: programming servos in robot c

Thanks all who helped me, I got it working. Turns out that naming the servos as the first random thing that came to mind worked:

#pragma config(Hubs, S1, HTMotor, HTMotor, HTServo, none)
#pragma config(Motor, mtr_S1_C1_1, motorD, tmotorNormal, openLoop)
#pragma config(Motor, mtr_S1_C1_2, motorE, tmotorNormal, openLoop)
#pragma config(Motor, mtr_S1_C2_1, motorF, tmotorNormal, openLoop)
#pragma config(Motor, mtr_S1_C2_2, motorG, tmotorNormal, openLoop)
#pragma config(Servo, srvo_S1_C3_1, rofl, tServoNormal)
#pragma config(Servo, srvo_S1_C3_2, mao, tServoNormal)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

/////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tele-Operation Mode Code Template
//
// This file contains a template for simplified creation of an tele-op program for an FTC
// competition.
//
// You need to customize two functions with code unique to your specific robot.
//
/////////////////////////////////////////////////////////////////////////////////////////////////////

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


/////////////////////////////////////////////////////////////////////////////////////////////////////
//
// initializeRobot
//
// Prior to the start of tele-op mode, you may want to perform some initialization on your robot
// and the variables within your program.
//
// In most cases, you may not have to add any code to this function and it will remain "empty".
//
/////////////////////////////////////////////////////////////////////////////////////////////////////

void initializeRobot()
{
// Place code here to sinitialize servos to starting positions.
// Sensors are automatically configured and setup by ROBOTC. They may need a brief time to stabilize.
servo[rofl] = 119;
servo[mao] = 135;
return;
}


/////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Main Task
//
// The following is the main code for the tele-op robot operation. Customize as appropriate for
// your specific robot.
//
// Game controller / joystick information is sent periodically (about every 50 milliseconds) from
// the FMS (Field Management System) to the robot. Most tele-op programs will follow the following
// logic:
// 1. Loop forever repeating the following actions:
// 2. Get the latest game controller / joystick settings that have been received from the PC.
// 3. Perform appropriate actions based on the joystick + buttons settings. This is usually a
// simple action:
// * Joystick values are usually directly translated into power levels for a motor or
// position of a servo.
// * Buttons are usually used to start/stop a motor or cause a servo to move to a specific
// position.
// 4. Repeat the loop.
//
// Your program needs to continuously loop because you need to continuously respond to changes in
// the game controller settings.
//
// At the end of the tele-op period, the FMS will autonmatically abort (stop) execution of the program.
//
/////////////////////////////////////////////////////////////////////////////////////////////////////

task main()
{
initializeRobot();

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

bMotorReflected[motorD]=true;

bMotorReflected[motorF]=true;

while (true)
{
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
//// ////
//// Add your robot specific tele-op code here. ////
//// ////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////

// Insert code to have servos and motors respond to joystick and button values.
getJoystickSettings(joystick);
motor[motorD] = joystick.joy1_y1;
motor[motorE] = joystick.joy1_y2;

motor[motorG] = 0.65*joystick.joy2_y1;
motor[motorF] = 0.65*joystick.joy2_y2;

if(joy2Btn(8))
{
servo[rofl] = 190;
servo[mao] = 65;
}
else
{
servo[rofl] = 119;
servo[mao] = 135;
}
// Look in the ROBOTC samples folder for programs that may be similar to what you want to perform.
// You may be able to find "snippets" of code that are similar to the functions that you want to
// perform.
}
}
:D

Author:  10nisman94 [ Mon May 18, 2009 8:08 pm ]
Post subject:  Re: programming servos in robot c

:downloading: :downloading:

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