View unanswered posts | View active topics It is currently Mon Dec 22, 2014 3:12 am






Reply to topic  [ 4 posts ] 
Any comments on my pneumatic switch code? 
Author Message
Rookie

Joined: Mon Aug 20, 2007 4:01 am
Posts: 28
Post Any comments on my pneumatic switch code?
Hello,
I'd be interested to hear any suggestions for improving my code for this motorised pneumatic switch. I don't write much RobotC and want to understand if there are better more compact and efficient methods.

Image

It uses the usual NXT motor to move the switch from one end to the other. It does not use a touch sensor to determine the position but checks to see if the motor has stopped rotating. In the code below, which works, the touch senor on port one controlls motor A, and so on for the other two ports.

Any sugestions for code improvements welcome.
Thanks
Karl.

Code:
#pragma config(Sensor, S1,     Touch1,              sensorTouch)
#pragma config(Sensor, S2,     Touch2,              sensorTouch)
#pragma config(Sensor, S3,     Touch3,              sensorTouch)
#pragma config(Motor,  motorA,          Motor1,        tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  motorB,          Motor2,        tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  motorC,          Motor3,        tmotorNormal, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#pragma platform(NXT)

int valvePos[4];     // array to store the current valve positions
int leverLFT = 50;   // motor left power
int leverRGT = -50;  // motor right power

// move the motor until it is stopped.
void valveOn(short MotorX, int Direction)
{
  int motorBack = 40;   // motor relief
  int mpos;
  int menc;
  int mpow;

  // rotate the motor until it has stopped (stays at the same value for more than 25msec)
  while(mpos != menc) {
    mpos = nMotorEncoder[MotorX];
    motor[MotorX] = Direction;
    wait1Msec(25);  // increase this delay slightly if the valve doesn't move
    menc = nMotorEncoder[MotorX];
  }
  motor[MotorX] = 0; //wait1Msec(10);
  // back the lever of a little to stop the motor from squealing.
  if (Direction == leverRGT) {  // right
    mpos = nMotorEncoder[MotorX] + motorBack;
    mpow = leverLFT;
    }
  else {                        //left
    mpos = nMotorEncoder[MotorX] - motorBack;
    mpow = leverRGT;
  }
  nMotorEncoderTarget[MotorX] = mpos;
  while (nMotorEncoder[MotorX] != mpos) {
    motor[MotorX] = mpow;
    }
  motor[MotorX] = 0;
  return;
}

void valveMove(short MotorX, int Valve, int Position) {
  //  valve positions:
  //    4 unkown (on startup)
  //    1 lever right   motor: -50
  //    2 lever left    motor: +50

  // if the valve state is unknown (4), set it up
  if (valvePos[Valve] == 4) {
    valveOn(MotorX,leverRGT);
    valvePos[Valve] = 1;
    return;
  }

  // if current position is the same as the new position, don't bother
  if (valvePos[Valve] == Position) {
    return;
  }

  switch (Position) {
    case 1:                                 //    1 lever right
      valveOn(MotorX,leverRGT);
      break;
    case 2:                                 //    2 lever left
      valveOn(MotorX,leverLFT);
      break;
    }
  valvePos[Valve] = Position;
  return;
}


task main() {

  // setup motor stuff
  nPowerDownDelayMinutes  = 240;  // 4 hours
  bNoPowerDownOnACAdaptor = true;
  nMotorPIDSpeedCtrl[Motor1] = mtrSpeedReg;
  nMotorPIDSpeedCtrl[Motor2] = mtrSpeedReg;
  nMotorPIDSpeedCtrl[Motor3] = mtrSpeedReg;
  nMaxRegulatedSpeedNxt = 750;

  nMotorEncoder[Motor1] = 0;
  nMotorEncoder[Motor2] = 0;
  nMotorEncoder[Motor3] = 0;

  // valve positions are unkown at startup
  valvePos[1] = 4;
  valvePos[2] = 4;
  valvePos[3] = 4;
 
  //set valves to start positions
  valveMove(Motor1,1,2);       // valveMove(motor, valvePos[no], position)
  valveMove(Motor2,2,1);
  valveMove(Motor3,3,1);

  wait1Msec(1000);
  PlaySound(soundBeepBeep);

  // main loop, move the relivant valve when the touch sensor is pressed.
  while (nNxtButtonPressed != 3) {
    nxtDisplayBigStringAt (0,60,"M1: %d  ", valvePos[1]);
    nxtDisplayBigStringAt (0,40,"M2: %d  ", valvePos[2]);
    nxtDisplayBigStringAt (0,20,"M3: %d  ", valvePos[3]);

    if (SensorValue(Touch1) == 1) {
      if (valvePos[1] == 1) {
        valveMove(Motor1,1,2);
        wait1Msec(100);
      }
      else {
        valveMove(Motor1,1,1);
        wait1Msec(100);
      }
    }
    if (SensorValue(Touch2) == 1) {
      if (valvePos[2] == 1) {
        valveMove(Motor2,2,2);
        wait1Msec(100);
      }
      else {
        valveMove(Motor2,2,1);
        wait1Msec(100);
      }
    }
    if (SensorValue(Touch3) == 1) {
      if (valvePos[3] == 1) {
        valveMove(Motor3,3,2);
        wait1Msec(100);
      }
      else {
        valveMove(Motor3,3,1);
        wait1Msec(100);
      }
    }
    wait1Msec(200);
  }
}


Tue Jan 03, 2012 7:59 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3297
Location: Rotterdam, The Netherlands
Post Re: Any comments on my pneumatic switch code?
Hey there,

Code looks fine but I would change a couple of things:
use the tMotors type, not short to pass motor names. So
Code:
void valveMove(short MotorX, int Valve, int Position)

becomes
Code:
void valveMove(tMotors MotorX, int Valve, int Position)

I would allow a small amount of wiggle room for comparing the current position to the previous in case the motor vibrates quickly.

Other than that it looks fine to me :)

- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Tue Jan 03, 2012 1:03 pm
Profile WWW
Rookie

Joined: Mon Aug 20, 2007 4:01 am
Posts: 28
Post Re: Any comments on my pneumatic switch code?
Hello Xander,

Thanks for the comments, I shall change to the tMotors type.

What do you mean by more wriggle room?

Karl.


Wed Jan 04, 2012 6:42 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1372
Post Re: Any comments on my pneumatic switch code?
I spotted some other potential problems:
1. In the function valveOn, you need to initialize the local variables menc and mpos before you can reference them.
2. Like what Xander said, you should not do exact comparison between mpos and menc because even if the motor stopped, it may still vibrate back and forth a little bit (that's what wiggle room means). You should do something like while (abs(mpos - menc) < threshold).


Wed Jan 04, 2012 7:38 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 4 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.