View unanswered posts | View active topics It is currently Fri Nov 28, 2014 4:47 pm






Reply to topic  [ 5 posts ] 
Robot freeezes while running a task during tel-op 
Author Message
Rookie

Joined: Mon Feb 27, 2012 5:26 pm
Posts: 20
Post Robot freeezes while running a task during tel-op
The issue is when the light on a motor port is activated via the program the whole robot will freeze but after the light is done flashing works normally can anyone please help me run both at the same time with out an issue? The code is down below.



Code:
#pragma config(Hubs,  S1, HTMotor,  HTMotor,  HTMotor,  HTServo)
#pragma config(Hubs,  S3, HTMotor,  none,     none,     none)
#pragma config(Sensor, S1,     ,               sensorI2CMuxController)
#pragma config(Sensor, S2,     SEEKER,         sensorHiTechnicIRSeeker1200)
#pragma config(Sensor, S3,     ,               sensorI2CMuxController)
#pragma config(Motor,  motorA,          leftnxt,       tmotorNXT, openLoop)
#pragma config(Motor,  motorB,          rightnxt,      tmotorNXT, openLoop)
#pragma config(Motor,  motorC,           ,             tmotorNXT, openLoop)
#pragma config(Motor,  mtr_S1_C1_1,     left,          tmotorTetrix, openLoop)
#pragma config(Motor,  mtr_S1_C1_2,     left2,         tmotorTetrix, openLoop)
#pragma config(Motor,  mtr_S1_C2_1,     right,         tmotorTetrix, openLoop, reversed)
#pragma config(Motor,  mtr_S1_C2_2,     right2,        tmotorTetrix, openLoop, reversed)
#pragma config(Motor,  mtr_S1_C3_1,     arm,           tmotorTetrix, openLoop)
#pragma config(Motor,  mtr_S1_C3_2,     linear,        tmotorTetrix, openLoop)
#pragma config(Motor,  mtr_S3_C1_1,     light,         tmotorTetrix, openLoop)
#pragma config(Motor,  mtr_S3_C1_2,     motorK,        tmotorTetrix, openLoop)
#pragma config(Servo,  srvo_S1_C4_1,    tilt,                 tServoStandard)
#pragma config(Servo,  srvo_S1_C4_2,    servo2,               tServoNone)
#pragma config(Servo,  srvo_S1_C4_3,    servo3,               tServoNone)
#pragma config(Servo,  srvo_S1_C4_4,    servo4,               tServoNone)
#pragma config(Servo,  srvo_S1_C4_5,    servo5,               tServoNone)
#pragma config(Servo,  srvo_S1_C4_6,    servo6,               tServoNone)
#pragma config(SrvoPosition,  Position01,            131, 128, 128, 128, 128, 226, 128, 128)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#include "JoystickDriver.c"

task main()
{
  while (1 == 1)
  {
    getJoystickSettings(joystick);


    if (joystick.joy2_y2 < 10 && joystick.joy2_y2 > -10)  /* Makes left side wheels move in tandem and same speed */
    {                                                                                 // Makes motors = joystick move
                                                                                     //   but if at 10 or negative 10 stop
      motor[right] = 0;
      motor[right2] = 0;  // just states if joystick = 10 or -10 motor = 0

    }
    else
    {
      motor[right] = joystick.joy2_y2;
         motor[right2] = joystick.joy2_y2; // if it is above 10 or -10 then the motor power = the joystick.
    }


    if (joystick.joy2_y1 < 10 && joystick.joy2_y1 > -10)  /* Makes left side wheels move in tandem and same speed */
    {                                                                                 // Makes motors = joystick move
                                                                                     //   but if at 10 or negative 10 stop
      motor[left] = 0;
      motor[left2] = 0; // joystick if 10 or -10 = motor power = 0

    }
    else
    {
      motor[left] = joystick.joy2_y1;  // if it is above 10 or -10 then the motor power = the joystick.
      motor[left2] = joystick.joy2_y1;

    }
    while (joy2Btn(6) == 1 )
    {
       motor[right] = joystick.joy2_y2/3;
         motor[right2] = joystick.joy2_y2/3;
       motor[left] = joystick.joy2_y1/3;
         motor[left2] = joystick.joy2_y1/3;
      }
        if (joystick.joy1_y1 < 5 && joystick.joy1_y1 > -5) //motor = 0 if at 5 or -5
    {
      motor[arm] = 0;


    }
    else
    {
      motor[arm] =  joystick.joy1_y1/3;

    }
{
      if(joy1Btn(1) == 1)                  // If Joy1-Button2 is pressed:
        {
          motor[arm] =  -4;             // ring is Grabbed
                                // Can use the debugger to help find these positions
        }

    // Infinite loop:
{
      if(joy1Btn(9) == 1)                  // If Joy1-Button2 is pressed:
        {
          servo[tilt] =  45;             // ring is Grabbed
                                // Can use the debugger to help find these positions
        }
    {
      if(joy1Btn(4) == 1)                  // If Joy1-Button2 is pressed:
        {
          servo[tilt] =  1;             // ring is Grabbed
                                // Can use the debugger to help find these positions
        }

      {
    if(joy1Btn(2) == 1)                  // If Joy1-Button2 is pressed:
        {
          servo[tilt] =  217;             // ring is Grabbed
                                        // Can use the debugger to help find these positions
        }
        {
          if(joy1Btn(3) == 1)                  // Open position us Joy1btn 3
                                                // if button 3 is pressed
          {
            servo[tilt] = 95;

          }






        }

     if(joy1Btn(7) == 1)                  // If Joy1-Button7is pressed:
    {
      motor[linear] = -100;              // Turn Motor linear at full power
    }
     else                            // If Joy1-Button7 is NOT pressed:
    {
      motor[linear] = 0;                 // Turn linear Off
    }
     if(joy1Btn(7))
    {
      motor[linear] = -100;   // linear is run at a power level of -100. arm down
      wait1Msec(100);
    }

if(joy1Btn(5) == 1)                  // If Joy1-Button7is pressed:
    {
      motor[linear] = -25;              // Turn Motor linear at full power
    }
     else                            // If Joy1-Button7 is NOT pressed:
    {
      motor[linear] = 0;                 // Turn linear Off
    }
     if(joy1Btn(5))
    {
      motor[linear] = -25;   // linear is run at a power level of -100. arm down
      wait1Msec(100);
    }

    if(joy1Btn(6) == 1)                  // If Joy1-Button7is pressed:
    {
      motor[linear] = 25;              // Turn Motor linear at full power
    }
     else                            // If Joy1-Button7 is NOT pressed:
    {
      motor[linear] = 0;                 // Turn linear Off
    }
     if(joy1Btn(6))
    {
      motor[linear] = 25;   // linear is run at a power level of -100. arm down
      wait1Msec(100);
    }
 if(joy1Btn(8) == 1)                  // If Joy1-Button8 is pressed:
    {
      motor[linear] = 100;              // Turn Motor linear On at full power
    }
    else                            // If Joy1-Button8 is NOT pressed:
    {
      motor[linear] = 0;                 // Turn linear Off
    }
    if(joy1Btn(8))
    {
      motor[linear] = 100;   // linear is run at a power level of 100.
      wait1Msec(100);
    }

   
   
     if(joy2Btn(5) == 1)                  // If Joy1-Button8 is pressed:
    {
int count = 0;
while(count < 10)
{
  motor[light] = 100;   // linear is run at a power level of 100.
      wait1Msec(250);
       motor[light] = 0;   // linear is run at a power level of 100.
      wait1Msec(250);
count = count + 1;
}

}

   
   

}}}}}}


Thu Jan 10, 2013 4:36 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Robot freeezes while running a task during tel-op
If you want to do multiple things at the same time, you should not have any nested while loops. I have cleaned up your code. Since I don't really know your intention on some of the operations, I tried my best to guess what you want. Here is the cleaned up version.
Code:
task main()
{
    bool bLightMotorOn = false;
    long nextPeriod = nPgmTime;

    while (1 == 1)
    {
        getJoystickSettings(joystick);

        // ******************************************
        // Task 1: Get joystick readings and do tank drive.
        // ******************************************
        int leftPower, rightPower;

        if (joystick.joy2_y2 < 10 && joystick.joy2_y2 > -10)  /* Makes left side wheels move in tandem and same speed */
        {
            rightPower = 0;
        }
        else
        {
            rightPower = joystick.joy2_y2;
        }

        if (joystick.joy2_y1 < 10 && joystick.joy2_y1 > -10)  /* Makes left side wheels move in tandem and same speed */
        {
            leftPower = 0;
        }
        else
        {
            leftPower = joystick.joy2_y1;
        }

        if (joy2Btn(6) == 1 )
        {
            rightPower /= 3;
            leftPower /= 3;
        }

        motor[right] = rightPower;
        motor[right2] = rightPower;
        motor[left] = leftPower;
        motor[left2] = leftPower;

        // ******************************************
        // Task 2: Get joystick readings and move the arm.
        // ******************************************
        int armPower;

        if (joystick.joy1_y1 < 5 && joystick.joy1_y1 > -5) //motor = 0 if at 5 or -5
        {
            armPower = 0;
        }
        else
        {
            armPower = joystick.joy1_y1/3;
        }

   //
        // Don't know what this code is trying to do?
        //
        if (joy1Btn(1) == 1)              // If Joy1-Button2 is pressed:
        {
            armPower = -4;                // ring is Grabbed
                                          // Can use the debugger to help find these positions
        }

        motor[arm] = armPower;

        // ******************************************
        // Task 3: Read button states and determine grabber tilt angle.
        // (I am guessing here since I don't know your real intention)
        // ******************************************
        int grabberTilt;

        if (joy1Btn(9) == 1)              // If Joy1-Button2 is pressed:
        {
            grabberTilt = 45;             // ring is Grabbed
                                          // Can use the debugger to help find these positions
        }
        else if (joy1Btn(4) == 1)         // If Joy1-Button2 is pressed:
        {
            grabberTilt =  1;             // ring is Grabbed
                                          // Can use the debugger to help find these positions
        }
        else if (joy1Btn(2) == 1)         // If Joy1-Button2 is pressed:
        {
            grabberTilt = 217;            // ring is Grabbed
                                          // Can use the debugger to help find these positions
        }
        else if (joy1Btn(3) == 1)         // Open position us Joy1btn 3
                                          // if button 3 is pressed
        {
            grabberTilt = 95;
        }

        servo[tilt] = grabberTilt;

        // ******************************************
        // Task 4: Read button states and determine "linear power".
        // (Don't know what this is for so I am guessing here)
        // ******************************************
        int linearPower;

        if (joy1Btn(7) == 1)              // If Joy1-Button7is pressed:
        {
            linearPower = -100;           // Turn Motor linear at full power
        }
        else if (joy1Btn(5) == 1)
        {
            linearPower = -25;            // Turn Motor linear at full power
        }
        else if (joy1Btn(6) == 1)
        {
            linearPower = 25;             // Turn Motor linear at full power
        }
        else if (joy1Btn(8) == 1)
        {
            linearPower = 100;            // Turn Motor linear at full power
        }
        else                              // If Joy1-Button7 is NOT pressed:
        {
            linearPower = 0;              // Turn linear Off
        }

        motor[linear] = linearPower;

        // ******************************************
        // Task 5: Don't understand what you are trying to do here.
        // So I am trying my best to guess here.
        // ******************************************

        if (joy2Btn(5) == 1)              // If Joy1-Button8 is pressed:
        {
            if (nPgmTime >= nextPeriod)
            {
                bLightMotorOn = !bLightMotorOn;
                nextPeriod += 250;
            }
            motor[light] = bLightMotorOn? 100: 0;
        }
        else
        {
            motor[light] = 0;
        }

        // Loop delay (i.e. runs approx. 10 loops a second).
        wait1Msec(100);
    }
}

Notice that by indenting the code properly, it is a lot easier to read and understand. Also, I did not change any of your comments although most of them are obviously wrong.
Each of the code sections (starting with the // ******** comment block) is a "task". In order for these tasks to run "concurrently", none of these "tasks" should have any while-loop or wait statement in it (i.e. None of the tasks should block the other tasks from making progress).
I am not sure what the last task is trying to do. It seems that if you pressed joystick 2 button 5, then some "light" motor will be pulsing between full power and off for 250 msec each. I also don't understand the "count" variable. It loops for 10 times but then go back to the main loop and execute it again for another 10 times and so on. The only difference with my modification and your code is that my modification requires you to press and hold joystick 2 button 5 whereas yours will run the 10 loop even though the button is released. If you really meant that way, it could be changed easily by detecting the edge event of pressing button 5.


Thu Jan 10, 2013 7:04 pm
Profile
Novice

Joined: Sun Oct 21, 2012 10:01 pm
Posts: 76
Post Re: Robot freeezes while running a task during tel-op
I would recommend formatting and indenting your code to make it more readable; logic errors will pop out easily if you can see the structure. Go to the edit menu in RobotC, down to "Code Formatting," and click "Format Whole File."

One spot in your code really jumps out at me:
Code:
if(joy2Btn(5) == 1)                  // If Joy1-Button8 is pressed:
{
   int count = 0;
   while(count < 10)
   {
      motor[light] = 100;   // linear is run at a power level of 100.
      wait1Msec(250);
      motor[light] = 0;   // linear is run at a power level of 100.
      wait1Msec(250);
      count = count + 1;
   }
}

Whenever button 8 on controller 1 is pressed, the program enters a while loop. Each repetition of the loop, it waits half a second; if you repeat ten times, the rest of your teleop program will not respond for five seconds.


Thu Jan 10, 2013 7:08 pm
Profile
Rookie

Joined: Mon Feb 27, 2012 5:26 pm
Posts: 20
Post Re: Robot freeezes while running a task during tel-op
MHTS great of thanks I tried it and edited the comment text but next time I upload a tel-op it should be much easier for anyone to help me. I can't be more grateful for what you did for me thank you. To amcerbu I will have to try that during next week the portion of the code you pulled out was the issue exactly, but I didn't know why exactly. For another issue I am trying encoders for autonomous only and need help due to I got the robotc to scan all 4 encoders, but the bot still veers off by a considerable distance sometimes like either a slight over or under turn and short or long on a straight away. I tried reading some online sources and tried using an online course but, all it had that was useful was resources from carnegie mellon great as they maybe they are pretty limited in explaining if something is wrong. I am happy about using the ir seeker being able to act as a "program chooser". If I post the code on Monday can I have a quick response since the 19th is a another ftc meet I'm trying to not panic the night before all help would be great. Should I start a new topic for the forum instead?


Fri Jan 11, 2013 6:40 pm
Profile
Rookie

Joined: Mon Feb 27, 2012 5:26 pm
Posts: 20
Post Re: Robot freeezes while running a task during tel-op
I have the power level pretty low but if I need further explanation I will ask. I will try my best to use the pervious post to teach myself so I don't take up too much of your time. But for accuracy the tolerance is about a half inch I think. Visually looking at it is pretty small. The power level though to be more specific is at 20 with pid enabled.


Fri Jan 11, 2013 9:35 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 5 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

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.