View unanswered posts | View active topics It is currently Thu Dec 18, 2014 8:33 am






Reply to topic  [ 4 posts ] 
is the code correct 
Author Message
Rookie

Joined: Wed Dec 21, 2011 4:22 pm
Posts: 26
Post is the code correct
the robot works like this there is 2motors on the bottom, and 2 motor on the top for the arm i am trying to do is to control the robots movement with the channels 1, and 2 i want to use the channel 2 to move forward and channel 1 to move left and right and channel 6 to move the arm up and down.


task main()
{
while (1 == 1)
{
motor [left motor] = vexRT [Ch2];
motor [rihgt motor] = vexRT [ch2];

if (vexRT[Ch1] == 1)
{
motor [left motor] = -100;
motor [right motor] = 100;
}
if (vexRT[Btn6U]== 1)
{
motor [armmotorleft] = -40;
motor [armmotorright] = 40;
}
else if (vexRT[Btn6D])
{
motor [armmotorleft] = 40;
motor [armmotorright] = -40;
}
else
{
motor [armmotorleft] = 0;
motor [armmotorright] = 0;
}
}
}


Wed Dec 21, 2011 4:29 pm
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: is the code correct
Well, you can check if the code is correct by putting it on your robot and testing it out. By looking at the code however, I do not think that it will work as I assume you want it to work. Here are my comments:
(By the way, next time you post code, you can put it in code tags so it's nice and formatted)
Code:
task main()
{
  while (1 == 1)
  {
    motor [left motor] = vexRT [Ch2];
    motor [rihgt motor] = vexRT [ch2];//you spelled "right motor" wrong here
    //Technically, it should be "Ch2" instead of ch2, but I don't know if that will stop it from compiling
    //I don't know if you're reflected your motors, but if they're symmetrical on both sides of the robot, then they should be reflected.

    if (vexRT[Ch1] == 1)
    {
      motor [left motor] = -100;
      motor [right motor] = 100;
    }
    //What are you trying to do here? Turn the robot left and right? If so, then the second that the joystick deviates from 0,
      your robot will start jerking depending on whether you're pushing up on the joystick. I'll post how I think that this should be handled below.

    if (vexRT[Btn6U]== 1) //This works is fine but you don't need to include the "== 1" part if you don't want to.
    // I don't know what exactly is going on here, so I can't comment, but It looks alright.
    {
      motor [armmotorleft] = -40;
      motor [armmotorright] = 40;
    }
    else if (vexRT[Btn6D])
    {
      motor [armmotorleft] = 40;
      motor [armmotorright] = -40;
    }
    else
    {
      motor [armmotorleft] = 0;
      motor [armmotorright] = 0;
    }
  }
}


I would recommend implementing the drive that you described (usually referred to as arcade drive) like this:
Code:
while(true){
    motor [leftmotor] = vexRT[Ch2] + vexRT[Ch1];
    motor [rightmotor] = vexRT[ch2] - vexRT[Ch1];
}

_________________
sudo rm -rf /


Wed Dec 21, 2011 6:22 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1359
Post Re: is the code correct
magicode wrote:
I would recommend implementing the drive that you described (usually referred to as arcade drive) like this:
Code:
while(true){
    motor [leftmotor] = vexRT[Ch2] + vexRT[Ch1];
    motor [rightmotor] = vexRT[ch2] - vexRT[Ch1];
}

I am not familiar with the Cortex so I could be wrong but I am assuming the motor range for Cortex is still between -100 and 100. What about the joystick range? If it is the same as NXT, it is between -128 to 127. If so, you probably need to scale the numbers so that the sum and difference of them still fall into the -100 to 100 range. Or you will have severely clipped zones.
I have a library function that does Arcade Drive. It will take care of smoothly scaling the full range of X and Y axes of the joystick to the full power range of the left and right motors.
Code:
/**
 *  The BOUND macro limits the value (n) within the bounds between the given
 *  low (l) and high (h).
 */
#define BOUND(n,l,h)            (((n) < (l))? (l): ((n) > (h))? (h): (n))

//
// Joystick input macros.
//
#ifndef DEADBAND_INPUT_THRESHOLD
    #define DEADBAND_INPUT_THRESHOLD 20
#endif

/**
 *  These macros ignore input value (n) that is within the DEADBAND_THRESHOLD.
 *  This is necessary because analog joysticks do not always centered at zero.
 *  So if the joystick is at the rest position, we will consider it zero even
 *  though the value is non-zero but within DEADBAND_THRESHOLD.
 */
#define DEADBAND(n,t)           ((abs(n) > (t))? (n): 0)
#define DEADBAND_INPUT(n)       DEADBAND(n, DEADBAND_INPUT_THRESHOLD)

/**
 *  The NORMALIZE macro transforms a value (n) in the range between (sl) and
 *  (sh) to the range between (tl) and (th).
 */
#define MOTOR_MIN_VALUE         -100
#define MOTOR_MAX_VALUE         100

#define NORMALIZE(n,sl,sh,tl,th) (int)(((long)(n) - (sl))*((th) - (tl))/((sh) - (sl)) + (tl))
#define NORMALIZE_DRIVE(x,m,n)  NORMALIZE(x, m, n, \
                                          MOTOR_MIN_VALUE, MOTOR_MAX_VALUE)
#define JOYSTICK_POWER(n)       NORMALIZE_DRIVE(DEADBAND_INPUT(n), -128, 127)

/**
 *  This function sets power of the motors for arcade drive.
 *
 *  @param drivePower Specifies the drive power.
 *  @param turnPower Specifies the turn power.
 */
void
DriveArcade(
    int drivePower,
    int turnPower
    )
{
    int leftPower, rightPower;

    drivePower = BOUND(drivePower, MOTOR_MIN_VALUE, MOTOR_MAX_VALUE);
    turnPower = BOUND(turnPower, MOTOR_MIN_VALUE, MOTOR_MAX_VALUE);
    if (drivePower + turnPower > MOTOR_MAX_VALUE)
    {
        //
        // Forward right:
        //  left = drive + turn - (drive + turn - MOTOR_MAX_VALUE)
        //  right = drive - turn - (drive + turn - MOTOR_MAX_VALUE)
        //
        leftPower = MOTOR_MAX_VALUE;
        rightPower = -2*turnPower + MOTOR_MAX_VALUE;
    }
    else if (drivePower - turnPower > MOTOR_MAX_VALUE)
    {
        //
        // Forward left:
        //  left = drive + turn - (drive - turn - MOTOR_MAX_VALUE)
        //  right = drive - turn - (drive - turn - MOTOR_MAX_VALUE)
        //
        leftPower = 2*turnPower + MOTOR_MAX_VALUE;
        rightPower = MOTOR_MAX_VALUE;
    }
    else if (drivePower + turnPower < MOTOR_MIN_VALUE)
    {
        //
        // Backward left:
        //  left = drive + turn - (drive + turn - MOTOR_MIN_VALUE)
        //  right = drive - turn - (drive + turn - MOTOR_MIN_VALUE)
        //
        leftPower = MOTOR_MIN_VALUE;
        rightPower = -2*turnPower + MOTOR_MIN_VALUE;
    }
    else if (drivePower - turnPower < MOTOR_MIN_VALUE)
    {
        //
        // Backward right:
        //  left = drive + turn - (drive - turn - MOTOR_MIN_VALUE)
        //  right = drive - turn - (drive - turn - MOTOR_MIN_VALUE)
        //
        leftPower = 2*turnPower + MOTOR_MIN_VALUE;
        rightPower = MOTOR_MIN_VALUE;
    }
    else
    {
        leftPower = drivePower + turnPower;
        rightPower = drivePower - turnPower;
    }

    motor[leftMotor] = leftPower;
    motor[rightMotor] = rightPower;

    return;
}   //DriveArcade

task main()
{
    while (true)
    {
        getJoystickSettings(joystick);
        DriveArcade(JOYSTICK_POWER(joystick.joy1_y1), JOYSTICK_POWER(joystick.joy1_x1);
        wait1Msec(100);
    }
}

I would have attached an Excel graph showing the power mapping curve of the above algorithm but I can't figure out how to attach a picture directly to the post.


Thu Dec 22, 2011 1:40 am
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: is the code correct
Both the joystick range and motor power on the cortex ranges from -127 -to 127. If the value is above or below that, ROBOTC takes care of it by setting it to the highest or lowest value respectively. I was just suggesting a very low complexity code that would work nicely. Of course, It can be improved upon a lot.

_________________
sudo rm -rf /


Thu Dec 22, 2011 2:16 am
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.