View unanswered posts | View active topics It is currently Wed Jul 30, 2014 12:32 am






Reply to topic  [ 2 posts ] 
Using the NXT Motor as a Bump Sensor.... 
Author Message
Rookie

Joined: Mon Aug 20, 2007 4:01 am
Posts: 28
Post Using the NXT Motor as a Bump Sensor....
Hello,
In the code below, I am trying to have the robot react when the motor has changed speeds, from running into somthing. The code appears to work, but the biggest problem is when the motor is running backwards, when the nMotorEncoder is counting down. This robot has a touch Senor on 1 (to change the motors directon) and a NXT motor on A.

Code:
#pragma config(Sensor, S1,     Touch1,              sensorTouch)
#pragma config(Motor,  motorA,          MotorA,        tmotorNormal, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

int bToggle = 0;
long Counter = 0;
int timeTaken;
bool ignoreMonitor;

void valveOn(short MotorX, int Direction)
{
  if (Direction == 1) {
    motor[motorA] = -80;
  }
  else {
    motor[motorA] = 80;
  }
}

task MonitorTimeTaken() {
  // detect when the motor has been slowed
  int normalTimeTaken = 60; // increase this to decrease sensitivity
  while (true) {
    if (timeTaken > normalTimeTaken && ignoreMonitor==false) {
      nMotorEncoder[MotorA] = 0;
      Counter=0;
      timeTaken = normalTimeTaken-10;
      PlayTone(784, 10);   
      nxtDisplayString(4, "    -- BUMP --  ");
      wait1Msec(300);     
      nxtDisplayString(4, "                ");
    } 

  }
}

task TimeTaken() {
 // measure the amount of time Counter increments by 40
  int timeStart;
  long check;
  while (true) {
    timeStart = time1[T1];   
    check = Counter + 40;   
    while (Counter < check) {
        wait1Msec(1);
    }       
    timeTaken = time1[T1] - timeStart;
    nxtDisplayString(5, "CO: %d, CHK: %d", Counter, check);
    nxtDisplayString(6, "TT: %d, dir: %d", timeTaken, bToggle );
    wait1Msec(10);
  }
}

task CounterIncrement() {

  long motorPos;
  while (true) {
    motorPos = nMotorEncoder[MotorA];
    Counter = abs(motorPos);  // the counter is always a positive number

    nxtDisplayString(1, "MA: %d", motorPos);
    nxtDisplayString(2, "CC: %d", Counter);
    wait1Msec(10);
  }
}

task main() {

    // setup motor stuff
  nPowerDownDelayMinutes  = 240;  // 4 hours
  bNoPowerDownOnACAdaptor = true;
  nMotorPIDSpeedCtrl[MotorA] = mtrSpeedReg;
  nMotorEncoder[MotorA] = 0;
  ignoreMonitor = true;

  StartTask(CounterIncrement);
  StartTask(TimeTaken);
  StartTask(MonitorTimeTaken);   
  wait1Msec(300);
  ignoreMonitor = false;
 
  while (nNxtButtonPressed != 3) {

    if (SensorValue(Touch1) == 1) {
      bToggle = 1;
    }
    else {
      bToggle = 0;
    }

    valveOn(MotorA, bToggle);
    wait1Msec(200);
  }
}


Now, I want Counter to always be going up, but if I change task CounterIncrement() to the following:

Code:
task CounterIncrement() {

  long motorPos;
  long posMotor;
  while (true) {
    motorPos = nMotorEncoder[MotorA];
    if (motorPos != posMotor) {
      Counter++;
    }
    posMotor=motorPos;

    nxtDisplayString(1, "MA: %d", motorPos);
    nxtDisplayString(2, "CC: %d", Counter);
    wait1Msec(10);
  }
}


the senititivity (int normalTimeTaken) is dramatically decreased, and bumps are not detected properly. How can I have the Counter always incrementing, or is there a way of counting 40 ticks of the motor no matter which direction it is going?

Thanks
Karl


Sun Nov 13, 2011 12:49 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: Using the NXT Motor as a Bump Sensor....
When the motors reverse, try adding this
Code:
bMotorReflected[MotorA]=true;


That command reverses everything related to the motor. Which means that even when it is going backwards, it will still be counting up. There have been problems with counting negative degrees in RobotC.

When you want it to be normal again, just set it to false instead of true.

_________________
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.


Sun Nov 13, 2011 1:46 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 2 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.