View unanswered posts | View active topics It is currently Sat Oct 25, 2014 7:37 pm






Reply to topic  [ 5 posts ] 
three abs() make an NXT lockup? 
Author Message
Rookie

Joined: Wed Jan 09, 2013 7:25 pm
Posts: 20
Post three abs() make an NXT lockup?
I was working with a student through an issue the other night.

In working through troubleshooting encoder values the abs() was used twice.

Now, just as quick try and see, we added a third abs into the function.

As soon as the program tried to run, the NXT would freeze.

If I took one of the abs() back out, the program would run.

I don't know if there was additional use of abs() in the task main, but we did try and use it three times in this one function. And that is where I uncovered the issue.

This was version 3.54


Fri Apr 19, 2013 3:59 pm
Profile
Senior Roboticist
User avatar

Joined: Wed Sep 28, 2011 10:13 pm
Posts: 630
Location: If I told you, I'd have to kill you.
Post Re: three abs() make an NXT lockup?
That's odd. Can you post the malfunctioning code?

_________________
A.K.A. inxt-generation
Self-proclaimed genius, and future world dominator.
My Brickshelf Folder
"Don't they teach recreational mathematics anymore?" - The Tenth Doctor
Bow down to Nikola Tesla, King of the Geek Gods.


Fri Apr 19, 2013 4:05 pm
Profile WWW
Rookie

Joined: Wed Jan 09, 2013 7:25 pm
Posts: 20
Post Re: three abs() make an NXT lockup?
I will have to try and re-create it. It will be a bit.
(or there is a problem with our NXT that just happened to hit the right memory register each time).


Fri Apr 19, 2013 4:11 pm
Profile
Rookie

Joined: Wed Jan 09, 2013 7:25 pm
Posts: 20
Post Re: three abs() make an NXT lockup?
(Don't try and fix the code, because this revision didn't work yet - this is just for the repro of the abs() causing the NXT to lockup.)
This is the entirety, not just the errant function.

The StallCode function is where you will find it
(again, don't comment on the math, that isn't the point - the bug / lockup is).


====================================
Code:
#pragma config(Hubs,  S1, HTMotor,  HTMotor,  none,     none)
#pragma config(Sensor, S1,     ,               sensorI2CMuxController)
#pragma config(Motor,  mtr_S1_C1_1,     Right,         tmotorTetrix, PIDControl, reversed, encoder)
#pragma config(Motor,  mtr_S1_C1_2,     Left,          tmotorTetrix, PIDControl, encoder)
#pragma config(Motor,  mtr_S1_C2_1,     motorC,        tmotorTetrix, openLoop)
#pragma config(Motor,  mtr_S1_C2_2,     ForkLift,      tmotorTetrix, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

//All comments to most of this code is in TeleOpJarrod.c
#include "JoystickDriver.c"


int deadZone = 15;
long Peg1 = 2196;//peg 1 encoder value
long Peg2 = 5203;//peg 2 encoder value
long Peg3 = 8584;//peg 3 encoder value
long Zero = 0;
long goPosition;

long tooLong = 300;  // millisecond threshod for time to pass
long sigMove = 200; // How many encoder ticks is a 'significant' movement

//variables used for stall code need to be initialized
bool powerHasBeenOn[] = {false, false, false}; // True after significant power has been applied
long timeLastSigMove[] = {0, 0, 0}; // Time last significant move occurred
long encLastSigMove[] = {0, 0, 0}; // Encoder reading at last significant move

int StallCode(tMotor motorSentTo, int wantedPower)
{
   int motorIndex;  //index value for the arrays we are storing values in.
   switch(motorSentTo) //which motor power is being sent to
   {
      case Left: // This is the name of one of the motors as referenced in the configuraiton.
         motorIndex = 0;
         break;
      case Right:
         motorIndex = 1;
         break;
      case ForkLift:
         motorIndex = 2;
         break;
      default:
         break;
   }

   if (abs(wantedPower) < deadZone)  // Power below threshold, mark as no power.
      {
         powerHasBeenOn[motorIndex] = false;

         return wantedPower;
      }

   if (powerHasBeenOn[motorIndex] == false)  // Power transitioned above threshold, start monitoring power.   Allow whatever power desired this time.
      {
         powerHasBeenOn[motorIndex] = true;
         timeLastSigMove[motorIndex]    = time1[T1];
         encLastSigMove[motorIndex] = abs(nMotorEncoder[motorSentTo]);

         return wantedPower;
      }

   if ( (abs( encLastSigMove[motorIndex] - abs(nMotorEncoder[motorSentTo]))) > sigMove)  // Moved far enough to be considered significant, mark
      {
         timeLastSigMove[motorIndex]   = time1[T1];
         encLastSigMove[motorIndex] = abs(nMotorEncoder[motorSentTo]);

         return wantedPower;
      }

   if ( (time1[T1] - timeLastSigMove[motorIndex]) > tooLong )  // Time since last significant move too long, stalled
      {
         PlayTone(724,5);
         return 0;
      }

   return wantedPower;   // Haven’t moved far enough yet to be significant but haven’t timed out yet
}


task Joystick2()
{
   while(true)
   {
      getJoystickSettings(joystick);
      //D-pad fork-lift control
      if(joystick.joy2_TopHat ==0)
      {
         motor[ForkLift] = StallCode(ForkLift, 75);
         //motor[ForkLift] = 100;
      }
      else if(joystick.joy2_TopHat ==4)
      {
         motor[ForkLift] = StallCode(ForkLift, -75);
         //motor[ForkLift] = -100;
      }
      else
      {
         motor[ForkLift] = StallCode(ForkLift, 0);
         //motor[ForkLift] = 0;
      }
      //going to peg 1
      if(joystick.joy2_Buttons == 2)
      {
         goPosition = (Peg1 - nMotorEncoder[ForkLift]);
         if (goPosition > 0) // go up
         {
            while(nMotorEncoder[ForkLift] < Peg1) // to position
            {
               motor[ForkLift] = 75;  // speed
            }
            motor[ForkLift] = 0; // stop
         }
         else if (goPosition < 0) // go down
         {
            while(nMotorEncoder[ForkLift] > Peg1) // to position
            {
               motor[ForkLift] = -75; // speed
            }
            motor[ForkLift] = 0; // stop
         }
      }
      //going to peg 2
      else if(joystick.joy2_Buttons == 1)
      {
         goPosition = (Peg2 - nMotorEncoder[ForkLift]);
         if (goPosition > 0) // go up
         {
            while(nMotorEncoder[ForkLift] < Peg2) // to position
            {
               motor[ForkLift] = 75;  // speed
            }
            motor[ForkLift] = 0; // stop
         }
         else if (goPosition < 0) // go down
         {
            while(nMotorEncoder[ForkLift] > Peg2) // to position
            {
               motor[ForkLift] = -75; // speed
            }
            motor[ForkLift] = 0; // stop
         }
      }
      //going to peg 3
      else if(joystick.joy2_Buttons == 4)
      {
         goPosition = (Peg3 - nMotorEncoder[ForkLift]);
         if (goPosition > 0) // go up
         {
            while(nMotorEncoder[ForkLift] < Peg3) // to position
            {
               motor[ForkLift] = 75;  // speed
            }
            motor[ForkLift] = 0; // stop
         }
         else if (goPosition < 0) // go down
         {
            while(nMotorEncoder[ForkLift] > Peg3) // to position
            {
               motor[ForkLift] = -75; // speed
            }
            motor[ForkLift] = 0; // stop
         }
      }

      else if(joystick.joy2_Buttons == 3)
      {
         goPosition = (Zero - nMotorEncoder[ForkLift]);
         if (goPosition > 0) // go up
         {
            while(nMotorEncoder[ForkLift] > Zero) // to position
            {
               motor[ForkLift] = -75;  // speed
            }
            motor[ForkLift] = 0; // stop
         }
         else if (goPosition < 0) // go down
         {
            while(nMotorEncoder[ForkLift] < Zero) // to position
            {
               motor[ForkLift] = 75; // speed
            }
            motor[ForkLift] = 0; // stop
         }
      }
   }
}


task main()
{
   waitForStart();
   StartTask(Joystick2);
   ClearTimer(T1);
   nMotorEncoder[ForkLift] = 0;
   //slow mode
   while(true)
   {
      getJoystickSettings(joystick);
      if(joy1Btn(7) == 1)
      {
         if(abs(joystick.joy1_y1) > deadZone)
         {
            motor[Left] = StallCode(Left, (joystick.joy1_y1) / 5);
         }
         else
         {
            motor[Left] = StallCode(Left, 0);
         }

         if(abs(joystick.joy1_y2) > deadZone)
         {
            motor[Right] = StallCode(Right, (joystick.joy1_y2) / 5);
         }
         else
         {
            motor[Right] = StallCode(Right, 0);
         }
      }
      else
      {
         if(abs(joystick.joy1_y1) > deadZone)
         {
            motor[Left] = StallCode(Left, (joystick.joy1_y1));
         }
         else
         {
            motor[Left] = StallCode(Left, 0);
         }

         if(abs(joystick.joy1_y2) > deadZone)
         {
            motor[Right] = StallCode(Right, (joystick.joy1_y2));
         }
         else
         {
            motor[Right] = StallCode(Right, 0);
         }
      }
   }
}


Fri Apr 19, 2013 4:22 pm
Profile
Site Admin
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 596
Post Re: three abs() make an NXT lockup?
Code:
(abs( encLastSigMove[motorIndex] - abs(nMotorEncoder[motorSentTo])))


At a glance, the problem may be with the parentheses placement. You have an opening parentheses on the first absolute command, but you don't close it until the end of the if statement's condition. Instead, try this:

Code:
if ((abs(encLastSigMove[motorIndex]) - abs(nMotorEncoder[motorSentTo])) > sigMove)


Let us know if this solves the issue for you, and if not we will continue to debug this further.

_________________
Check out our Blog! And our Facebook page!
Need help? Take a look at our Wiki and our Forums.

I just met you,
And this is crazy,
But here's my code now,
So fix it, maybe?
~ Carly Rae Jepsen parody


Mon Apr 22, 2013 1:24 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.