Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RobotC - Programming an Elevator

I'm designing and programming an elevator-like robot for a high school project. Could I possibly do anything to make this any simpler? Or better? I have attached a picture of my design that I made in AutoCAD Inventor with labels.

enter image description here

For those not familiar with RobotC or VEX (it is VERY similar to C and C++): the limit switches (limit1, limit2, ...) and bump switches (floor1, floor2, ...) are analog buttons and return a value of 0 if not pressed and 1 if pressed. The motor (mainMotor) rotates the gear which causes the mechanism to travel upwards on the slide. When the shaft sticking out the motor mechanism moves up and down, it presses limit switches and causes it to return a value of 1.

int callup [3];
int calldown [3];
int floorat[3];

    int main ()
    {

        if (SensorValue[limit1] == 1)
        {
            floorat[0] = 1;
        }
        else
        {
            floorat[0] = 0;
        }

        if (SensorValue[limit2] == 1)
        {
            floorat[1] = 1;
        }
        else
        {
            floorat[1] = 0;
        }

        if (SensorValue[limit3] == 1)
        {
            floorat[2] = 1;
        }
        else
        {
            floorat[2] = 0;
        }

        if (SensorValue[floor1] == 1)
        {
            calldown[0] = 1;
            SensorValue[LED1] = 1;
        }

        if (SensorValue[floor2] == 1 && floorat[2] == 1)
        {
            calldown[1] = 1;
            SensorValue[LED2] = 1;
        }

        if (SensorValue[floor2] == 1 && floorat[0] == 1)
        {
            callup[1] = 1;
            SensorValue[LED2] = 1;
        }

        if (SensorValue[floor3])
        {
            callup[2] = 1;
            SensorValue[LED3] = 1;
        }

        motors ();

    }


    void motors ()
    {

        if (callup[2] == 1 && floorat[2] == 1)
        {
            int x = 1;
            while (x < 3)
            {
                SensorValue[LED3] = 1;
                wait(0.5);
                SensorValue[LED3] = 0;
                wait(0.5);
            }
            callup[2] = 0;
            main ();
        }
        else if (callup[1] == 1 && floorat[1] == 1)
        {
            int x = 1;
            while (x < 3)
            {
                SensorValue[LED2] = 1;
                wait(0.5);
                SensorValue[LED2] = 0;
                wait(0.5);
            }
            callup[1] = 0;
            main ();
        }
        else if (callup[0] == 1 && floorat[0] == 1)
        {
            int x = 1;
            while (x < 3)
            {
                SensorValue[LED1] = 1;
                wait(0.5);
                SensorValue[LED1] = 0;
                wait(0.5);
            }
            callup[0] = 0;
            main ();
        }

        if (callup[2] == 1 && floorat[1] == 1 && calldown[0] == 0 || callup[2] == 1 && floorat[0] == 1 && callup[1] == 0)
        {
            startMotor(mainMotor, 60);
            untilTouch(limit3);
            stopMotor(mainMotor);
            callup[2] = 0;
            wait(1);
            main ();
        }

        if (callup[1] == 1 && floorat[0] == 1)
        {
            startMotor(mainMotor, 60);
            untilTouch(limit2);
            stopMotor(mainMotor);
            callup[1] = 0;
            wait(1);
            main();
        }

        if (calldown[1] == 1 && floorat[2] == 1)
        {
            startMotor(mainMotor, -60);
            untilTouch(limit2);
            stopMotor(mainMotor);
            calldown[1] = 0;
            wait(1);
            main();
        }

        if (calldown[0] == 1 && floorat[2] == 1 && calldown[1] == 0 || calldown[0] == 1 && floorat[1] == 1)
        {
            startMotor(mainMotor, -60);
            untilTouch(limit1);
            stopMotor(mainMotor);
            calldown[0] = 0;
            wait(1);
            main();
        }
    }

Although it shouldn't be a concern for this question, the 60 in the startMotor command is the speed of the motor, just to make it clearer.

Feel free to ask any more questions.

like image 941
Sam Javed Avatar asked Mar 22 '23 17:03

Sam Javed


1 Answers

Let's define what are the states of an elevator at a given moment:

An elevator can go up, down, or be idle.

enter image description here

The elevator is at a given floor and go from one floor to the other when it trigger a switch:

enter image description here

Now, if we translate this into some pseudo code (which should be easily translated to RobotC) :

enum elevator_status = { idle, down, up };
int currentfloor; //1, 2, 3 


switch(elevator_status)
{
    case idle:    
        //we check if a button is pressed and possibly go up or down           
        if(SensorValue(floor1))
        {         
             if(currentfloor > 1)
                elevator_status = down;               
        }
        else if(SensorValue(floor2))
        {
              if(currentfloor > 2)
                  elevator_status = down;
              else if(currentfloor < 2)
                  elevator_status = up;               
        }
        else if(SensorValue(floor3))
        {
              if(currentfloor < 3)
                  elevator_status = up;   
        }
        break;

    case up:
    case down:    
        //we check if we trigger a floor switch and stop the elevator
        if(SensorValue(limit1))
        {      
           currentfloor = 1;
           elevator_status = idle;
        }
        else if(SensorValue(limit2))
        {
           currentfloor = 2;
           elevator_status = idle;
        }
        else if(SensorValue(limit3))
        {
           currentfloor = 3;
           elevator_status = idle;
        }
        break;
}


//we set the speed of the motor
if(elevator_status == up)
{
   set_motorstate(cw);
)
else if(elevator_status == down)
{
   set_motorstate(ccw);   
}
else if(elevator_status == idle)
{
   set_motorstate(idle);   
}

Note : in this code the elevator will only take care of new up and down floor calls when the elevator is idle. It does not store up and down call while it is moving and go there later. I do not know if it was a requirement for you.

like image 127
tigrou Avatar answered Apr 02 '23 13:04

tigrou