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

Coding Non-Sequentially
http://www.robotc.net/forums/viewtopic.php?f=11&t=5586
Page 1 of 1

Author:  DeskLamp [ Tue Mar 05, 2013 11:54 pm ]
Post subject:  Coding Non-Sequentially

Hi there. I'm in a PLTW run course that utilizes the vex cortex along with the metal vex kits. I was wondering if it were possible to code non-sequentially. I've been tasked to design an elevator that uses three bump switches as call signals to make a motor move a platform up and down. When I code it, even after putting each function in it's own while loop with different sensor values, it still wants me to press each button in sequence. Here's an example of one of the codes for a single floor.

//Elevator is on floor 3//

while(sensorvalue(sonar)>18)
{
untiltouch(floor2);
startmotor(elevator, -127);
untilsonargreaterthan(12,sonar);
stopmotor(elevator);
}

while(sensorvalue(limit)==1)
{
untiltouch(floor1);
startmotor(elevator,-60);
untilsonarlessthan(8,sonar);
stopmotor(elevator);
}

For above, I would have to hit floor two, wait for it to reach floor two, and then manually reset the elevator to floor 3 to then press the floor one button to get it to work. Is it even possible to change this?

Author:  Coder A [ Wed Mar 06, 2013 9:14 am ]
Post subject:  Re: Coding Non-Sequentially

Could you post your entire code please? Thanks.

Author:  DeskLamp [ Wed Mar 06, 2013 7:34 pm ]
Post subject:  Re: Coding Non-Sequentially

Sure. Here's everything I wrote before I figured out the problem.

#pragma config(Sensor, dgtl1, red, sensorLEDtoVCC)
#pragma config(Sensor, dgtl2, yellow, sensorLEDtoVCC)
#pragma config(Sensor, dgtl3, green, sensorLEDtoVCC)
#pragma config(Sensor, dgtl4, floor3, sensorTouch)
#pragma config(Sensor, dgtl5, floor2, sensorTouch)
#pragma config(Sensor, dgtl6, floor1, sensorTouch)
#pragma config(Sensor, dgtl7, limit, sensorTouch)
#pragma config(Sensor, dgtl8, sonar, sensorSONAR_cm)
#pragma config(Sensor, dgtl11, quad, sensorQuadEncoder)
#pragma config(Motor, port2, elevator, tmotorVex269, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

task main()
{
while(1==1)
{
//Elevator on Floor 3//
while(SensorValue(limit)==1)
{
untilTouch(floor1);
startMotor(elevator,-60);
untilSonarLessThan(8,sonar);
stopMotor(elevator);
}
while(SensorValue(sonar)>18)
{
untilTouch(floor2);
startMotor(elevator,-60);
untilSonarLessThan(16,sonar);
stopMotor(elevator);
}
//Elevator on Floor 1//
while(SensorValue(quad)<299)
{
untilTouch(floor3);
startMotor(elevator,127);
untilTouch(limit);
stopMotor(elevator);
}
while(SensorValue(sonar)<12)
{
untilTouch(floor2);
startMotor(elevator,127);
untilSonarGreaterThan(12,sonar);
stopMotor(elevator);
}


}
}



And also, i've attached a few build pictures.

Attachments:
IMG_1149.JPG
IMG_1149.JPG [ 1.92 MiB | Viewed 22642 times ]
IMG_1148.JPG
IMG_1148.JPG [ 2.5 MiB | Viewed 22642 times ]

Author:  Coder A [ Thu Mar 07, 2013 6:37 pm ]
Post subject:  Re: Coding Non-Sequentially

Code:
task main()
{
  while(1==1)
  {
    //Elevator on Floor 3//
    while(SensorValue(limit)==1)
    {
      untilTouch(floor1);
      startMotor(elevator,-60);
      untilSonarLessThan(8,sonar);
      stopMotor(elevator);
    }
    while(SensorValue(sonar)>18)
    {
      untilTouch(floor2);
      startMotor(elevator,-60);
      untilSonarLessThan(16,sonar);
      stopMotor(elevator);
    }
    //Elevator on Floor 1//
    while(SensorValue(quad)<299)
    {
      untilTouch(floor3);
      startMotor(elevator,127);
      untilTouch(limit);
      stopMotor(elevator);
    }
    while(SensorValue(sonar)<12)
    {
      untilTouch(floor2);
      startMotor(elevator,127);
      untilSonarGreaterThan(12,sonar);
      stopMotor(elevator);
    }
  }
}



In the future, use the code tabs to make your code easier to understand. I'll try to work this out!

Author:  Coder A [ Thu Mar 07, 2013 6:43 pm ]
Post subject:  Re: Coding Non-Sequentially

Could you tell me the expected sensor values for each floor?

Author:  DeskLamp [ Thu Mar 07, 2013 6:54 pm ]
Post subject:  Re: Coding Non-Sequentially

Sure.

Floor one is precisely eight centimeters.

Floor two is about twelve to fourteen given the coasting on the elevator. (I had to replace a track because of a physical catch that would stop the elevator even at 127 motor power. With the new track it runs muuch smoother.)

Floor three is above nineteen centimeters.

I should be able to fiddle with these if they aren't quite on point. Thanks!

Author:  Coder A [ Thu Mar 07, 2013 7:44 pm ]
Post subject:  Re: Coding Non-Sequentially

What about limit and quad?

Also, do you want to be able to move to any floor from any other floor?

Sorry, but the more questions I ask, the more likely it is I will be able to help you.

Author:  DeskLamp [ Thu Mar 07, 2013 11:02 pm ]
Post subject:  Re: Coding Non-Sequentially

No problem! I'm the same way with questions. I haven't actually figured out how to use the quad, and i'm not sure it's necessary to use it anyway. The limit switch is on floor three. And yes, the objective is to be able to move from any floor to any floor. Please fill me in on what you do so I can learn to do it myself :D

Author:  Coder A [ Fri Mar 08, 2013 1:52 pm ]
Post subject:  Re: Coding Non-Sequentially

All right, here is some psuedo-code:

Code:
task main()
{
  short currentfloor = floor your elevator always starts on, presumably floor one;
  while(true)
  {
    switch(currentfloor)
    {
    case floor1:

      while(floor2button is not pressed and floor3button is not pressed){DO NOTHING}

      if(floor2button is pressed)
        go_to_floor2from1();
      else
        go_to_floor3from1();

      break;

    case floor2:

      while(floor1button is not pressed and floor3button is not pressed){DO NOTHING}
      if(floor1button is pressed)
        go_to_floor1from2();
      else
        go_to_floor3from2();

      break;

    case floor3;

      while(floor1button is not pressed and floor2button is not pressed){DO NOTHING}
      if(floor1button is pressed)
        go_to_floor1from3();
      else
        go_to_floor2from3();

      break;
    }
  }
}




You need to fill in the go_to_floorxfromx() functions with the commands that work. Also, these commands should write the new currentfloor value to it, so that when the switch runs again, it enters the correct block.

The reason this code works is that the switch enters the block for the current floor and waits for a button on another floor to be pressed, then goes to that floor and exits the switch to loop again.

I changed the 1==1 in the loop to true, as this just saves a compiler step and is easier to understand. Any always true statement works.


Finally, note that I used no multitasking, as it was not needed, and adding it would only cause confusion.

Tell me if this helps!

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