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

Encoders help asap please
http://robotc.net/forums/viewtopic.php?f=52&t=5249
Page 1 of 1

Author:  Casey890 [ Wed Dec 12, 2012 8:45 am ]
Post subject:  Encoders help asap please

#pragma config(Hubs, S1, HTMotor, HTMotor, none, none)
#pragma config(Sensor, S1, , sensorI2CMuxController)
#pragma config(Sensor, S2, Irseeker, sensorHiTechnicIRSeeker1200)
#pragma config(Sensor, S3, light, sensorNone)
#pragma config(Sensor, S4, sonar, sensorNone)
#pragma config(Motor, mtr_S1_C1_1, left1, tmotorTetrix, openLoop)
#pragma config(Motor, mtr_S1_C1_2, left2, tmotorTetrix, openLoop, encoder)
#pragma config(Motor, mtr_S1_C2_1, right1, tmotorTetrix, openLoop, reversed, encoder)
#pragma config(Motor, mtr_S1_C2_2, right2, tmotorTetrix, openLoop, reversed)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

#include "JoystickDriver.c"

task main()
{
nMotorEncoder[left2] = 0;//reset the value of encoder B to zero
nMotorEncoder[right1] = 0;//reset the value of encoder C to zero
nMotorEncoderTarget[left2] = 1400;//set the encoder target to 720
nMotorEncoderTarget[right1] = 1400;//set the encoder target to 720
motor[left2] = -32;//turn on motorB at 32% power
motor[right1] = -30;//turn on motorB at 30% power
motor[left1] = -32;//turn on motorB at 32% power
motor[right2] = -30;



while(nMotorRunState[left2] != runStateIdle)(nMotorRunState[right1] != runStateIdle);
{ /* This is an idle loop. The program waits until the condition is satisfied*/}
motor[left2] = 0; //Turn off motors
motor[right1] = 0;
motor[left1] = 0;
motor[right2] = 0;



nMotorEncoder[left2] = 0;//reset the value of encoder B to zero
nMotorEncoder[right1] = 0;//reset the value of encoder C to zero
nMotorEncoderTarget[left2] = 825;//set the encoder target to 825
nMotorEncoderTarget[right1] = 825;//set the encoder target to 825
motor[left2] = 32;//turn on motorB at 32% power
motor[right1] = -30;//turn on motorB at 30% power
motor[left1] = 32;//turn on motorB at 32% power
motor[right2] = -30;


while(nMotorRunState[left2] != runStateIdle)(nMotorRunState[right1] != runStateIdle);
{ /* This is an idle loop. The program waits until the condition is satisfied*/}
motor[left2] = 0; //Turn off motors
motor[right1] = 0;
motor[left1] = 0; //
motor[right2] = 0;


nMotorEncoder[left2] = 0;//reset the value of encoder B to zero
nMotorEncoder[right1] = 0;//reset the value of encoder C to zero
nMotorEncoderTarget[left2] = 2500;//set the encoder target to 2500
nMotorEncoderTarget[right1] = 2500;//set the encoder target to 2500
motor[left2] = -32;//turn on motorB at 32% power
motor[right1] = -30;//turn on motorB at 30% power
motor[left1] = -32;//turn on motorB at 32% power
motor[right2] = -30;


while(nMotorRunState[left2] != runStateIdle)(nMotorRunState[right1] != runStateIdle);
{ /* This is an idle loop. The program waits until the condition is satisfied*/}
motor[left2] = 0; //Turn off motors
motor[right1] = 0;
motor[left1] = 0;
motor[right2] = 0;





}

Attachments:
middle peg.c [2.91 KiB]
Downloaded 461 times

Author:  Casey890 [ Wed Dec 12, 2012 8:48 am ]
Post subject:  Re: Encoders help asap please

The goal is to go reverse for a set distance, then do a turn, and then go in reverse one time all using encoders. For an odd reason it runs until the encoder reaches the maximum amount of clicks only in reverse and will not execute any other parts of the code.

Author:  amcerbu [ Wed Dec 12, 2012 10:54 am ]
Post subject:  Re: Encoders help asap please

I'm not quite sure what you're trying to do here:
Code:
while(nMotorRunState[left2] != runStateIdle)(nMotorRunState[right1] != runStateIdle);

If you want to check whether both motors are in runStateIdle, use the AND (&&) command.
Code:
while((nMotorRunState[left2] != runStateIdle) && (nMotorRunState[right1] != runStateIdle));

Author:  JohnWatson [ Wed Dec 12, 2012 11:31 am ]
Post subject:  Re: Encoders help asap please

Code:
nMotorEncoderTarget[left2] = 1400;//set the encoder target to 720
nMotorEncoderTarget[right1] = 1400;//set the encoder target to 720
motor[left2] = -32;//turn on motorB at 32% power
motor[right1] = -30;//turn on motorB at 30% power


This will also cause problems. What you are saying here is 'set the encoder target to +1400, then move the motors in a negative direction'. The motors will turn on and the encoders will move away from the target, not towards it; eventually you will hit what is called the overflow for the encoders, at which point the values will 'roll over' to the opposite amount (for the NXT motors, this amount is -32767 to +32767; once -32767 is reached, the encoders will roll over to +32767). This is why you will see the motors run for a very long time and then suddenly stop; they are moving towards the -32767 rollover point and, once they overflow to +32767, they then stop.

Author:  Casey890 [ Thu Dec 27, 2012 2:33 pm ]
Post subject:  Re: Encoders help asap please

Should I just set the target to a negative number then to avoid the over flow and also now I have four motor encoders on the robot. However I am trying to be able to have the while that scans the encoders to meet the distance of like 720 for a half rotation or 1440 for a full rotation I want to be able to do a turn so the left side be a -100 power level and nmotorencoder target to = -720 and to have the right side be a 720 and motor power to = 100 is this the right way to do this and will it work?

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