View unanswered posts | View active topics It is currently Sat Dec 20, 2014 3:18 am






Reply to topic  [ 3 posts ] 
function did not run when variables are cleared 
Author Message
Rookie

Joined: Wed Jan 31, 2007 10:30 am
Posts: 3
Location: Hong Kong
Post function did not run when variables are cleared
Hi,

I have the following program to control a biped, the function walk_start() call 3 functions:
shift_b()
shift_a(90)
stand_b()

Whenever I compile the program and download, or clear all variables in the debugger and run, the stand_b() did not run properly, it just step through it and ended (i.e. did not loop in the while(nMotorRunState[motorB] != runStateIdle) ). However, if I re-run the program again, the while loop works and behave correctly. Cannot figure out why it never works when variables are cleared, could someone advise any possible causes, thanks a lot!

Code:
int shift_power1 = 20;
int shift_power2 = 50;
int shift_power3 = 30;
int stall_cnt_b;
int stall_cnt_limit_1 = 20;

task motor_b_stall_monitoring() { // Check every 20 msec to see if the motor encoder has moved. If not, increment a counter
  int last_encoder_val;
  last_encoder_val = nMotorEncoder[motorB];
  while (true) {
    int encoder_delta;
    wait1Msec(20);
    encoder_delta = (nMotorEncoder[motorB] - last_encoder_val);
    if ((encoder_delta > 2) || (encoder_delta < -2)) { // a large delta means motor rotated
       stall_cnt_b = 0;
       last_encoder_val = nMotorEncoder[motorB];
    } else {
      if (stall_cnt_b < 1000)
        ++stall_cnt_b;
    }
  }
}

void shift_b() {
   nMotorEncoderTarget[motorC] = 60;//40
   motor[motorC] = shift_power1;
   nMotorEncoderTarget[motorB] = 25;
   motor[motorB] = -shift_power2;
   while(nMotorRunState[motorB] != runStateIdle) {};

   StartTask(motor_b_stall_monitoring);
   while (stall_cnt_b < stall_cnt_limit_1) {
      nMotorEncoderTarget[motorB] = 1;
      motor[motorB] = -shift_power3;
   }
   StopTask(motor_b_stall_monitoring);

   wait1Msec(1000);
   nMotorEncoderTarget[motorC] = 60;
   motor[motorC] = -shift_power1;
   while(nMotorRunState[motorC] != runStateIdle) {};
}


void shift_a(int the_angle) {
   nMotorEncoderTarget[motorA] = the_angle;
   motor[motorA] = -95;
   while(nMotorRunState[motorA] != runStateIdle) {};
}

void stand_b() {
   nMotorEncoderTarget[motorB] = nMotorEncoder[motorB];
   motor[motorB] = shift_power2;
   while(nMotorRunState[motorB] != runStateIdle) {};
}



void walk_start() {
   shift_b();
   shift_a(90); // c stride forward
   stand_b();
}


void init_motor() {
   nMotorEncoder[motorA] = 0;
   nMotorEncoder[motorB] = 0;
   nMotorEncoder[motorC] = 0;
   bFloatDuringInactiveMotorPWM = false;
   nMotorPIDSpeedCtrl = mtrSpeedReg;
}

void reset_pose()
{
   nNxtButtonTask  = -2;   // Grab control of the buttons
   int nBtn;
   bool keep_looping = true;
   while (keep_looping)   {
     while ((nBtn = nNxtButtonPressed) == -1) {}; // wait for button press
      nMotorEncoderTarget[motorA] = 1;
     switch (nBtn)     {
        case kLeftButton:
                  motor[motorA] = 100;
                  while(nMotorRunState[motorA] != runStateIdle) {};
                 break;
         case kRightButton:
                  motor[motorA] = -100;
                  while(nMotorRunState[motorA] != runStateIdle) {};
                  break;
         case kEnterButton: // reset encoder reading to 0
                  nMotorEncoder[motorA] = 0;
                  wait1Msec(1000);
                  keep_looping = false;
                  break;
      }
      nxtDisplayString(1, "%03d", nMotorEncoder[motorA]);
  }
}

task main() {
   init_motor();
   reset_pose();
   walk_start();
}


Fri Jan 04, 2008 10:56 am
Profile WWW
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 615
Post 
"Clear All" debugger command also wipes out the setting of the 'nMotorPIDSpeedCtrl' variable which sets it to no speed regulation. The "stand_b" function uses precise motor positioning which will only work if speed regulation is enabled. When you rerun your program, speed regulation gets re-enabled.


Wed Mar 05, 2008 1:38 am
Profile
Rookie

Joined: Wed Jan 31, 2007 10:30 am
Posts: 3
Location: Hong Kong
Post function did not run when variables are cleared
Hi Dick,

Thanks for your reply. I do not understand even if it wipes the setting of nMotorPIDSpeed Ctrl, why it is not set again when the program goes through the "init_motor()" function as called from "main()" each time?

Rgds,
meow


Wed Mar 05, 2008 9:30 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 3 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.