|
Page 1 of 1
|
[ 4 posts ] |
|
| Author |
Message |
|
themigthy
Rookie
Joined: Wed Dec 21, 2011 4:22 pm Posts: 26
|
 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 |
|
 |
|
magicode
Moderator
Joined: Tue Sep 14, 2010 9:19 pm Posts: 495
|
 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) I would recommend implementing the drive that you described (usually referred to as arcade drive) like this:
_________________ sudo rm -rf /
|
| Wed Dec 21, 2011 6:22 pm |
|
 |
|
MHTS
Guru
Joined: Sun Nov 15, 2009 5:46 am Posts: 1023
|
 Re: is the code correct
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 |
|
 |
|
magicode
Moderator
Joined: Tue Sep 14, 2010 9:19 pm Posts: 495
|
 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 |
|
|
|
Page 1 of 1
|
[ 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
|
|