Go to Post ps... it is better to have built a robot and lost than to ever played basketball at all - Andy Baker [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rating: Thread Rating: 9 votes, 5.00 average. Display Modes
  #1   Spotlight this post!  
Unread 07-04-2014, 13:29
Spacemonkey619 Spacemonkey619 is offline
Registered User
None #4899
Team Role: Programmer
 
Join Date: Jan 2013
Rookie Year: 2012
Location: California
Posts: 7
Spacemonkey619 is an unknown quantity at this point
How to loop peices of code forever?

Hello I am a programmer for my school's robotics team and engineering class and i ran into a code problem. A team project is to program a claw bot that when a button is pressed it will follow a line with encoders and with the claw bot grab a box line track backwards and let go of the box, it is supposed to do this forever when the button at the beginning is pressed. Not only that but when a button is pressed while its looping it will do this task one more time before ending the program. So my problem is that i can't get the code to run forever when the button is pressed, what happens is that the line track moves forever and never moves on to the next step. Here is how my code looks below.

PS: I know i didn't include the part when the button is pressed again it runs one more time. It's not included because that's not the problem.








#pragma config(Sensor, in1, pot, sensorPotentiometer)
#pragma config(Sensor, in3, LineLeft, sensorLineFollower)
#pragma config(Sensor, in4, LineRight, sensorLineFollower)
#pragma config(Sensor, in5, LineCenter, sensorLineFollower)
#pragma config(Sensor, dgtl1, touch, sensorTouch)
#pragma config(Sensor, dgtl2, Encoder, sensorQuadEncoder)
#pragma config(Sensor, dgtl7, Encoder2, sensorQuadEncoder)
#pragma config(Motor, port2, arm, tmotorServoContinuousRotation, openLoop)
#pragma config(Motor, port3, elbow, tmotorServoContinuousRotation, openLoop)
#pragma config(Motor, port7, rightMotor, tmotorServoContinuousRotation, openLoop, reversed)
#pragma config(Motor, port8, leftMotor, tmotorServoContinuousRotation, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

task main()
{



if(SensorValue(touch) == 0)
{
motor[arm] = 0;
}




if(SensorValue(touch) == 1)
{

while(true)// i also tried while(1 == 1)

{
motor[elbow] = -63;
wait1Msec(1500);
}

motor[elbow] = 0;
wait1Msec(100);





int threshold = 2000;

SensorValue[Encoder] = 0;


while(SensorValue[Encoder] < 210)
{




if(SensorValue(LineRight) > threshold)
{
// counter-steer right:
motor[leftMotor] = 50;
motor[rightMotor] = 0;
}
// CENTER sensor sees dark:
if(SensorValue(LineCenter) > threshold)
{
// go straight
motor[leftMotor] = 30;
motor[rightMotor] = 30;
}


if(SensorValue(LineLeft) > threshold)
{
// counter-steer left:
motor[leftMotor] = 0;
motor[rightMotor] = 50;
}

}
}

{
motor[arm] = 63;
wait1Msec(2000);

motor[elbow] = 63;
wait1Msec(200);

motor[rightMotor] = 0;
motor[leftMotor] = 0;
wait1Msec(1000);

motor[arm] = -63;
wait1Msec(1800);

motor[elbow] = -63;
wait1Msec(2500);

motor[elbow] = 0;
wait1Msec(1000);

motor[rightMotor] = -43;
motor[leftMotor] = -43;
wait1Msec(500);


}

int threshold = 2000;

SensorValue[Encoder] = 0;


while(SensorValue[Encoder] > -450)
{




if(SensorValue(LineRight) > threshold)
{
// counter-steer right:
motor[leftMotor] = -50;
motor[rightMotor] = 0;
}
// CENTER sensor sees dark:
if(SensorValue(LineCenter) > threshold)
{
// go straight
motor[leftMotor] = -30;
motor[rightMotor] = -30;
}


if(SensorValue(LineLeft) > threshold)
{
// counter-steer left:
motor[leftMotor] = 0;
motor[rightMotor] = -50;
}



}




}
  #2   Spotlight this post!  
Unread 07-04-2014, 14:13
RyanCahoon's Avatar
RyanCahoon RyanCahoon is offline
Disassembling my prior presumptions
FRC #0766 (M-A Bears)
Team Role: Engineer
 
Join Date: Dec 2007
Rookie Year: 2007
Location: Mountain View
Posts: 689
RyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond repute
Re: How to loop peices of code forever?

In your code, you have

Code:
while(true)// i also tried while(1 == 1)
{
	motor[elbow] = -63;
	wait1Msec(1500);
}
so you're on the right track. Both while(true) and while(1 == 1) will work (because 1 == 1 evaluates to true so it's just a more indirect way of writing the same thing).

However, the block of code inside the while(true) loop is the only thing that will run forever. So at the moment, your elbow motor will run forever, but not the rest of the code.

You should change your code so that the while(true) { ... } surrounds the entirety of the code that you want to run forever.
__________________
FRC 2046, 2007-2008, Student member
FRC 1708, 2009-2012, College mentor; 2013-2014, Mentor
FRC 766, 2015-, Mentor
  #3   Spotlight this post!  
Unread 07-04-2014, 18:10
Kevin Selavko's Avatar
Kevin Selavko Kevin Selavko is offline
Registered User
AKA: Voltonless
FRC #3260 (SHARP)
Team Role: Electrical
 
Join Date: Jan 2012
Rookie Year: 2008
Location: Beaver PA USA
Posts: 155
Kevin Selavko is on a distinguished road
Re: How to loop peices of code forever?

You may want to posibly try this, I changed some things that might make it work a little better(or fail miserably):
Code:
#pragma config(Sensor, in1, pot, sensorPotentiometer)
#pragma config(Sensor, in3, LineLeft, sensorLineFollower)
#pragma config(Sensor, in4, LineRight, sensorLineFollower)
#pragma config(Sensor, in5, LineCenter, sensorLineFollower)
#pragma config(Sensor, dgtl1, touch, sensorTouch)
#pragma config(Sensor, dgtl2, Encoder, sensorQuadEncoder)
#pragma config(Sensor, dgtl7, Encoder2, sensorQuadEncoder)
#pragma config(Motor, port2, arm, tmotorServoContinuousRotation, openLoop)
#pragma config(Motor, port3, elbow, tmotorServoContinuousRotation, openLoop)
#pragma config(Motor, port7, rightMotor, tmotorServoContinuousRotation, openLoop, reversed)
#pragma config(Motor, port8, leftMotor, tmotorServoContinuousRotation, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

int threshold = 2000;
void lineFollower(int speed);

task main()
{
	while(true)
	{

		if(SensorValue(touch) == 0)
		{
			motor[arm] = 0;
		}

		if(SensorValue(touch) == 1)
		{
			motor[elbow] = -63;
			wait1Msec(1500);
			motor[elbow] = 0;			

			SensorValue[Encoder] = 0;
			while(SensorValue[Encoder] < 210)
			{
				lineFollower(60);
			}

			motor[arm] = 63;  // Brackets were wraped around this, is there suposed to be a loop or somthing here?
			wait1Msec(2000);

			motor[elbow] = 63;
			wait1Msec(200);

			motor[rightMotor] = 0;
			motor[leftMotor] = 0;
			wait1Msec(1000);

			motor[arm] = -63;
			wait1Msec(1800);

			motor[elbow] = -63;
			wait1Msec(2500);

			motor[elbow] = 0;
			wait1Msec(1000);

			motor[rightMotor] = -43;
			motor[leftMotor] = -43;
			wait1Msec(500);
		}

		SensorValue[Encoder] = 0;

		while(SensorValue[Encoder] > -450)
		{
			lineFollower(-60);
		}
	}
}

void lineFollower(int speed)  // Can help clean up your code and make it easier to read
{

	if(SensorValue(LineRight) > threshold && SensorValue(LineCenter) > threshold)
	{ // Added a slight turn to have a bit of a gentler correction when the robot is not that far off the line
		motor[leftMotor] = speed;
		motor[rightMotor] = (speed / 2);
	}
	else if(SensorValue(LineLeft) > threshold && SensorValue(LineCenter) > threshold)
	{
		motor[leftMotor] = 0;
		motor[rightMotor] = (speed / 2);
	}
	else if(SensorValue(LineCenter) > threshold)
	{
		motor[leftMotor] = speed;
		motor[rightMotor] = speed;
	}
	else if(SensorValue(LineRight) > threshold)
	{
		motor[leftMotor] = speed;
		motor[rightMotor] = 0;
	}
	else if(SensorValue(LineLeft) > threshold)
	{
		motor[leftMotor] = 0;
		motor[rightMotor] = speed;
	}
	else
	{
		motor[leftMotor] = 0;
		motor[rightMotor] = 0;

	}
}
__________________
Team SHARP
Pittsburgh Regional Champions 2014
Vex Pittsburgh Excellence Award 2014
Vex Pittsburgh Champions 2014
Vex Maryland Champions 2014
Pittsburgh Regional Finalists 2013
Buckeye Regional Finalists 2013
  #4   Spotlight this post!  
Unread 08-04-2014, 12:48
Spacemonkey619 Spacemonkey619 is offline
Registered User
None #4899
Team Role: Programmer
 
Join Date: Jan 2013
Rookie Year: 2012
Location: California
Posts: 7
Spacemonkey619 is an unknown quantity at this point
Re: How to loop peices of code forever?

Yes I did put the while(true) brackets around the entire code, still it only moved the arm and line tracked forward forever. The code above me only went backwards forever. I can't believe these small problems sometimes.
  #5   Spotlight this post!  
Unread 08-04-2014, 13:06
RyanCahoon's Avatar
RyanCahoon RyanCahoon is offline
Disassembling my prior presumptions
FRC #0766 (M-A Bears)
Team Role: Engineer
 
Join Date: Dec 2007
Rookie Year: 2007
Location: Mountain View
Posts: 689
RyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond repute
Re: How to loop peices of code forever?

Quote:
Originally Posted by Spacemonkey619 View Post
still it only [...] line tracked forward forever
Your code will keep line tracking while SensorValue[Encoder] < 210 in the forward-moving phase and SensorValue[Encoder] > -450 in the backward-moving phase. How did you arrive at these conditions? Are you sure that Encoder counts in the positive direction when you're moving forward, and the negative direction when moving backward?
__________________
FRC 2046, 2007-2008, Student member
FRC 1708, 2009-2012, College mentor; 2013-2014, Mentor
FRC 766, 2015-, Mentor
  #6   Spotlight this post!  
Unread 08-04-2014, 15:53
Kevin Selavko's Avatar
Kevin Selavko Kevin Selavko is offline
Registered User
AKA: Voltonless
FRC #3260 (SHARP)
Team Role: Electrical
 
Join Date: Jan 2012
Rookie Year: 2008
Location: Beaver PA USA
Posts: 155
Kevin Selavko is on a distinguished road
Re: How to loop peices of code forever?

Quote:
Originally Posted by Spacemonkey619 View Post
Yes I did put the while(true) brackets around the entire code, still it only moved the arm and line tracked forward forever. The code above me only went backwards forever. I can't believe these small problems sometimes.
This should do more what you want maybe? Wait untill the button is pressed, move arm, follow line, move stuff around, reverse on line, wait for button again.

I also changed the encoders to an absulate value comparison so it wont matter if you have the tick calculations wrong.


Code:
#pragma config(Sensor, in1, pot, sensorPotentiometer)
#pragma config(Sensor, in3, LineLeft, sensorLineFollower)
#pragma config(Sensor, in4, LineRight, sensorLineFollower)
#pragma config(Sensor, in5, LineCenter, sensorLineFollower)
#pragma config(Sensor, dgtl1, touch, sensorTouch)
#pragma config(Sensor, dgtl2, Encoder, sensorQuadEncoder)
#pragma config(Sensor, dgtl7, Encoder2, sensorQuadEncoder)
#pragma config(Motor, port2, arm, tmotorServoContinuousRotation, openLoop)
#pragma config(Motor, port3, elbow, tmotorServoContinuousRotation, openLoop)
#pragma config(Motor, port7, rightMotor, tmotorServoContinuousRotation, openLoop, reversed)
#pragma config(Motor, port8, leftMotor, tmotorServoContinuousRotation, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

int threshold = 2000;
void lineFollower(int speed);
void stopAllMotors();

task main()
{
	while(true)
	{

		if(SensorValue(touch) == 0)
		{
			stopAllMotors();
		}

		if(SensorValue(touch) == 1)
		{
			motor[elbow] = -63;
			wait1Msec(1500);
			motor[elbow] = 0;

			SensorValue[Encoder] = 0;
			while(abs(SensorValue[Encoder]) < 210)
			{
				lineFollower(60);
			}

			motor[arm] = 63;  // Brackets were wraped around this, is there suposed to be a loop or somthing here?
			wait1Msec(2000);

			motor[elbow] = 63;
			wait1Msec(200);

			motor[rightMotor] = 0;
			motor[leftMotor] = 0;
			wait1Msec(1000);

			motor[arm] = -63;
			wait1Msec(1800);

			motor[elbow] = -63;
			wait1Msec(2500);

			motor[elbow] = 0;
			wait1Msec(1000);

			motor[rightMotor] = -43;
			motor[leftMotor] = -43;
			wait1Msec(500);

			SensorValue[Encoder] = 0;

			while(abs(SensorValue[Encoder]) < 450)
			{
				lineFollower(-60);
			}
		}
	}
}

void lineFollower(int speed)  // Can help clean up your code and make it easier to read
{

	if(SensorValue(LineRight) > threshold && SensorValue(LineCenter) > threshold)
	{ // Added a slight turn to have a bit of a gentler correction when the robot is not that far off the line
		motor[leftMotor] = speed;
		motor[rightMotor] = (speed / 2);
	}
	else if(SensorValue(LineLeft) > threshold && SensorValue(LineCenter) > threshold)
	{
		motor[leftMotor] = 0;
		motor[rightMotor] = (speed / 2);
	}
	else if(SensorValue(LineCenter) > threshold)
	{
		motor[leftMotor] = speed;
		motor[rightMotor] = speed;
	}
	else if(SensorValue(LineRight) > threshold)
	{
		motor[leftMotor] = speed;
		motor[rightMotor] = 0;
	}
	else if(SensorValue(LineLeft) > threshold)
	{
		motor[leftMotor] = 0;
		motor[rightMotor] = speed;
	}
	else
	{
		motor[leftMotor] = 0;
		motor[rightMotor] = 0;

	}
}

void stopAllMotors()
{
		motor[leftMotor] = 0;
		motor[rightMotor] = 0;
		motor[elbow] = 0;
		motor[arm] = 0;	
}
__________________
Team SHARP
Pittsburgh Regional Champions 2014
Vex Pittsburgh Excellence Award 2014
Vex Pittsburgh Champions 2014
Vex Maryland Champions 2014
Pittsburgh Regional Finalists 2013
Buckeye Regional Finalists 2013
  #7   Spotlight this post!  
Unread 09-04-2014, 13:02
Spacemonkey619 Spacemonkey619 is offline
Registered User
None #4899
Team Role: Programmer
 
Join Date: Jan 2013
Rookie Year: 2012
Location: California
Posts: 7
Spacemonkey619 is an unknown quantity at this point
Re: How to loop peices of code forever?

That code works but I think you misunderstand what i meant. I need this code to run forever once the button is pressed, once i press the button i can't press it again. The code has to keep running the same task once I press the button and the only reason I press the button again would be to end the while loop and stop the code.
__________________
1st place NXT CA Regional Mini Urban Challenge 2013
1st place NXT National Mini Urban Challenge 2013
1st place NXT CA Regional Mini Urban Challenge 2014
  #8   Spotlight this post!  
Unread 09-04-2014, 13:50
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,113
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Re: How to loop peices of code forever?

Quote:
Originally Posted by Spacemonkey619 View Post
...I think you misunderstand what i meant.
I think your description of what you want is unclear. You're using the phrase "loop forever", but you also say you want to "run it once more and stop". What is it that you want the robot to do repeatedly -- is it the entire process of shuttling along the line moving boxes from one end to the other? Will you ever want to give any other commands besides "start" and "stop after you drop the next box"?
  #9   Spotlight this post!  
Unread 09-04-2014, 14:02
Spacemonkey619 Spacemonkey619 is offline
Registered User
None #4899
Team Role: Programmer
 
Join Date: Jan 2013
Rookie Year: 2012
Location: California
Posts: 7
Spacemonkey619 is an unknown quantity at this point
Re: How to loop peices of code forever?

"The entire process of shuttling along the line moving boxes from one end to the other" Yes, So in the code should start up when a button is pressed and when it is the process should be looping forever.
__________________
1st place NXT CA Regional Mini Urban Challenge 2013
1st place NXT National Mini Urban Challenge 2013
1st place NXT CA Regional Mini Urban Challenge 2014
  #10   Spotlight this post!  
Unread 09-04-2014, 15:50
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,113
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Re: How to loop peices of code forever?

I don't know your level of knowledge so I can give you an appropriately tailored answer. My first thought is that this should be implemented as a state machine without any delays in it. The "start" state would do nothing, and would move to the "follow the line forward" when it sees the button press. When not in the "start" state I'd have the program continually check to see whether the button has just been pressed, and set a "prepare to stop" flag if so. If the flag is set, the final "drop the block" state would transition either to a "halted" state or back to the "start" state if you want to be able to start it up again.

Doing it sequentially with delays, as in the originally posted code, will make detecting the second button press very difficult.
  #11   Spotlight this post!  
Unread 09-04-2014, 16:01
Kevin Selavko's Avatar
Kevin Selavko Kevin Selavko is offline
Registered User
AKA: Voltonless
FRC #3260 (SHARP)
Team Role: Electrical
 
Join Date: Jan 2012
Rookie Year: 2008
Location: Beaver PA USA
Posts: 155
Kevin Selavko is on a distinguished road
Re: How to loop peices of code forever?

Last edit Im going to do
Code:
#pragma config(Sensor, in1, pot, sensorPotentiometer)
#pragma config(Sensor, in3, LineLeft, sensorLineFollower)
#pragma config(Sensor, in4, LineRight, sensorLineFollower)
#pragma config(Sensor, in5, LineCenter, sensorLineFollower)
#pragma config(Sensor, dgtl1, touch, sensorTouch)
#pragma config(Sensor, dgtl2, Encoder, sensorQuadEncoder)
#pragma config(Sensor, dgtl7, Encoder2, sensorQuadEncoder)
#pragma config(Motor, port2, arm, tmotorServoContinuousRotation, openLoop)
#pragma config(Motor, port3, elbow, tmotorServoContinuousRotation, openLoop)
#pragma config(Motor, port7, rightMotor, tmotorServoContinuousRotation, openLoop, reversed)
#pragma config(Motor, port8, leftMotor, tmotorServoContinuousRotation, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

int threshold = 2000;
bool continueRunning = true;
void lineFollower(int speed);
void stopAllMotors();
task checkForButtonPress();

task main()
{
	while(true)
	{

		if(SensorValue(touch) == 0)
		{
			stopAllMotors();
		}

		if(SensorValue(touch) == 1)
		{
			while(SensorValue(touch) == 1) // Wait for release
			{
			}
			StartTask(checkForButtonPress);
			while(continueRunning)
			{
				motor[elbow] = -63;
				wait1Msec(1500);
				motor[elbow] = 0;

				SensorValue[Encoder] = 0;
				while(abs(SensorValue[Encoder]) < 210)
				{
					lineFollower(60);
				}

				motor[arm] = 63;  // Brackets were wraped around this, is there suposed to be a loop or somthing here?
				wait1Msec(2000);

				motor[elbow] = 63;
				wait1Msec(200);

				motor[rightMotor] = 0;
				motor[leftMotor] = 0;
				wait1Msec(1000);

				motor[arm] = -63;
				wait1Msec(1800);

				motor[elbow] = -63;
				wait1Msec(2500);

				motor[elbow] = 0;
				wait1Msec(1000);

				motor[rightMotor] = -43;
				motor[leftMotor] = -43;
				wait1Msec(500);

				SensorValue[Encoder] = 0;

				while(abs(SensorValue[Encoder]) < 450)
				{
					lineFollower(-60);
				}
			}
		}
	}
}

void lineFollower(int speed)  // Can help clean up your code and make it easier to read
{

	if(SensorValue(LineRight) > threshold && SensorValue(LineCenter) > threshold)
	{ // Added a slight turn to have a bit of a gentler correction when the robot is not that far off the line
		motor[leftMotor] = speed;
		motor[rightMotor] = (speed / 2);
	}
	else if(SensorValue(LineLeft) > threshold && SensorValue(LineCenter) > threshold)
	{
		motor[leftMotor] = 0;
		motor[rightMotor] = (speed / 2);
	}
	else if(SensorValue(LineCenter) > threshold)
	{
		motor[leftMotor] = speed;
		motor[rightMotor] = speed;
	}
	else if(SensorValue(LineRight) > threshold)
	{
		motor[leftMotor] = speed;
		motor[rightMotor] = 0;
	}
	else if(SensorValue(LineLeft) > threshold)
	{
		motor[leftMotor] = 0;
		motor[rightMotor] = speed;
	}
	else
	{
		motor[leftMotor] = 0;
		motor[rightMotor] = 0;

	}
}

void stopAllMotors()
{
	motor[leftMotor] = 0;
	motor[rightMotor] = 0;
	motor[elbow] = 0;
	motor[arm] = 0;
}

task checkForButtonPress()
{continueRunning = true;
	while(true)
	{
		if(SensorValue(touch) == 1)
			continueRunning = false;

	}
}
__________________
Team SHARP
Pittsburgh Regional Champions 2014
Vex Pittsburgh Excellence Award 2014
Vex Pittsburgh Champions 2014
Vex Maryland Champions 2014
Pittsburgh Regional Finalists 2013
Buckeye Regional Finalists 2013
  #12   Spotlight this post!  
Unread 11-04-2014, 12:15
Spacemonkey619 Spacemonkey619 is offline
Registered User
None #4899
Team Role: Programmer
 
Join Date: Jan 2013
Rookie Year: 2012
Location: California
Posts: 7
Spacemonkey619 is an unknown quantity at this point
Re: How to loop peices of code forever?

Hey Kevin that Code almost works, It line follows back and fourth forever like its supposed to and stops when i press the button a second time. The only problem is that the arm keeps moving forever while its line tracking and the arm gets stuck because it never stops its first task. I'm trying to explain it right, the arm commands are supposed to stop once their done running for the time its given then switch to line following forward then move the arm a little you know to pick up the little box then switch to line following backwards.

I tried putting the commands inside where the line follower is because I thought since the line follower is working then the arm will too but the arm never stopped moving.
__________________
1st place NXT CA Regional Mini Urban Challenge 2013
1st place NXT National Mini Urban Challenge 2013
1st place NXT CA Regional Mini Urban Challenge 2014
  #13   Spotlight this post!  
Unread 08-05-2014, 22:14
yash101 yash101 is offline
Curiosity | I have too much of it!
AKA: null
no team
 
Join Date: Oct 2012
Rookie Year: 2012
Location: devnull
Posts: 1,191
yash101 is an unknown quantity at this point
Re: How to loop peices of code forever?

When I program in VEX, RobotC, I create a main loop. This loop will execute until it is killed somehow, or the break statement is used. Basically, you have an infinite loop (while(true) {...}) and that holds all your code.

In this loop, you only execute the command ONCE. Basically, the program loop will constantly check all inputs. If it notices any of the inputs high, for example, using a joystick, the program branches into another function, that runs the mechanism once. For servos, this is different. You assign a servo a position and it will stay there. You only signal the servo when you change positions (or not).

This way, everything will run together constantly.

It is sad that the VEX cortex does not support threading or tasks . It would be nice to run tasks parallel to each other. Does anyone by chance know how to bypass RobotC and actually access C10 (I think that's what it runs)? We are doing VEX robotics in eng. class, but I want to do things that are a bit harder!
  #14   Spotlight this post!  
Unread 08-05-2014, 23:56
apalrd's Avatar
apalrd apalrd is offline
More Torque!
AKA: Andrew Palardy (Most people call me Palardy)
VRC #3333
Team Role: College Student
 
Join Date: Mar 2009
Rookie Year: 2009
Location: Auburn Hills, MI
Posts: 1,347
apalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond repute
Re: How to loop peices of code forever?

Quote:
Originally Posted by yash101 View Post
It is sad that the VEX cortex does not support threading or tasks . It would be nice to run tasks parallel to each other. Does anyone by chance know how to bypass RobotC and actually access C10 (I think that's what it runs)? We are doing VEX robotics in eng. class, but I want to do things that are a bit harder!
There's quite a bit of confusion here, let me try to clear it up:


There is no inherent support for Tasks native to any processor, really. Tasks are almost always implemented by an operating system running on top of the processor, which runs a scheduler to schedule tasks when they are ready (on embedded systems they are usually scheduled by time rate). This is not a function of the processor hardware. However, there are ways to do tasks without an OS. Many micros have timers, which can be used for a number of things, including generating an interrupt request when they expire. This can be used to create a task by running code in the interrupt service routine. I usually see this used for very fast tasks, such as the OS kernel, but in basic systems it can be used to run fast code directly.

The Cortex has an ARM Cortex processor, so naturally it would run ARM opcodes. There is no real 'language' assigned to opcodes except the assembly language, which just gives meaningful names to opcodes (e.g. load, store, ... operations). C is a high level language on top of that, with a compiler that takes the C code and compiles it into opcodes for the particular processor family (in this case, ARM opcodes). As long as you can find a compiler to generate the opcodes for your processor, the language is usable.

RobotC does not actually use C standard code, it's quite bad with it (but it's gotten a lot better over the years). RobotC compiles using an internal compiler to RobotC bytecode, which is then run on a virtual machine on the Cortex. So, on the Cortex, the RobotC bytecode (opcodes) is executed by the RobotC interpreter, which is written in some language which was compiled into ARM opcodes. RobotC itself does have tasks, you can have many parallel threads, but you can't control the task rate precisely.
__________________
Kettering University - Computer Engineering
Kettering Motorsports
Williams International - Commercial Engines - Controls and Accessories
FRC 33 - The Killer Bees - 2009-2012 Student, 2013-2014 Advisor
VEX IQ 3333 - The Bumble Bees - 2014+ Mentor

"Sometimes, the elegant implementation is a function. Not a method. Not a class. Not a framework. Just a function." ~ John Carmack
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 02:47.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi