View unanswered posts | View active topics It is currently Fri Oct 30, 2020 6:36 am






Reply to topic  [ 11 posts ] 
Motor Encoders 
Author Message
Rookie

Joined: Mon Aug 25, 2014 2:10 am
Posts: 5
Post Motor Encoders
I am trying to make a motor copying program in which I will rotate 'motor A' a certain amount of degrees, and 'motor B' must rotate the same amount of degrees simultaneously. So while I rotate 'motor A' 30 degrees manually, 'motor B' should automatically rotate 30 degrees along with 'motor A'. I do not know how to achieve this, I have tried doing so with the following program, but to no avail.
This is for the Lego Mindstorms NXT 2.0
task main ()
{
int n1=nMotorEncoder[motorA];
nMotorEncoderTarget[motorB]=n1;
motor[motorB]=30;
wait10Msec(2000);
}


Mon Aug 25, 2014 2:17 am
Profile
Professor
User avatar

Joined: Sat Aug 31, 2013 9:15 am
Posts: 256
Post Re: Motor Encoders
Try putting you code in a loop and see what that does:
Code:
task main ()
{
  while (true)
  {
    int n1=nMotorEncoder[motorA];
    nMotorEncoderTarget[motorB]=n1;
    motor[motorB]=30;
    wait10Msec(2000);
  }
}


P.S. In the future, when posting code snippets, put it in between code tags. You can get them by clicking on the button marked "Code" in-between "Quote" and "List"

_________________
FTC Team 6100 Chariots of Fire - Programmer (2012-2013)
FTC Team 7468 Blue Chariots of Fire - Programmer (2013-2014)
FTC Team 7468 Blue Chariots of Fire - Mentor (2014-2015)
Check out our team website at http://bluechariotsoffire.com/.


Mon Aug 25, 2014 10:25 am
Profile
Rookie

Joined: Fri Sep 20, 2013 8:39 am
Posts: 38
Post Re: Motor Encoders
The loop is critical.

In the original example, assume there is a master (that you turn) and a slave that follows the master.

If we assume that both encoders have been set to the same value when the task starts. About 1 millisecond later, the program is setting the slave's destination to the master's current position. It is unlikely that the master has moved in that millisecond so the desired target position for the slave is the initial position of the master and the slave. The result is there is no movement.

BurninglLights program will update the position of the slave to the master every 20 seconds. You should find that the delay can be significantly reduced to the order of 10 milliseconds.

The Slave is likely to hunt for the final position. The wonders of control theory will assist to minimise the hunting. The simple solution is to create a dead zone where as long as the slave is say within 10 degrees of the master, the position should be accepted. A more complex solution is to tune the PID parameters.


Mon Aug 25, 2014 9:10 pm
Profile
Rookie

Joined: Mon Aug 25, 2014 2:10 am
Posts: 5
Post Re: Motor Encoders
I'm afraid the loop didn't work, Motor B, the slave, simply kept revolving for an unlimited amount of time no matter how many degrees I manually moved Motor A, the master. I tried rotating motor A in both directions, but to no avail. I was unable to understand what Sirius said when he referred to creating a 'dead zone'. I understand the concept, just do not know how the code will be written. Any assistance would be greatly appreciated.


Tue Aug 26, 2014 1:41 am
Profile
Rookie

Joined: Fri Sep 20, 2013 8:39 am
Posts: 38
Post Re: Motor Encoders
The Dead Zone

Abs(MasterEncoder - SlaveEncoder)>15 this creates a deadZone of 15 degrees

Try something like this (EV3 Version 4.25)
Code:
task main()
{
   int MasterEncoder;
   int SlaveEncoder;
   resetMotorEncoder(MasterDrive);
   resetMotorEncoder(SlaveDrive);
   setMotorBrakeMode(MasterDrive,motorCoast); //Put the Master in coast mode to allow it to spin
   setMotorBrakeMode(SlaveDrive,motorBrake); // Put the Slave in brake mode so it stops
   setMotorSpeed(MasterDrive,0);
   eraseDisplay();


   while(true) //the outer loop
   {
      MasterEncoder=getMotorEncoder(MasterDrive); //Get the position of the Master - needs to be updated prior to the internal loop
      SlaveEncoder=getMotorEncoder(SlaveDrive);    // Get the position of the Slave

      //Update position when not running inner loop
      displayBigTextLine(2,"Master %d",MasterEncoder);
      displayBigTextLine(5,"Slave  %d",SlaveEncoder);

      while(abs(MasterEncoder-SlaveEncoder)>15)  //The "Dead Zone" is 15 and it stops if it is within the Dead Zone
         // repeats until the two encoders are within 15 degrees

      {
         displayBigTextLine(2,"Master %d",MasterEncoder);
         displayBigTextLine(5,"Slave  %d",SlaveEncoder);
         setMotorTarget(SlaveDrive,MasterEncoder,100);
         sleep(50); //wait 50 msec before running the loop again
         MasterEncoder=getMotorEncoder(MasterDrive); // need to update the motor positions before re running the internal loop
         SlaveEncoder=getMotorEncoder(SlaveDrive);

      }
      setMotorSpeed(SlaveDrive,0); //force the Slave Drive to stop
   }

}


Tue Aug 26, 2014 5:43 am
Profile
Rookie

Joined: Mon Aug 25, 2014 2:10 am
Posts: 5
Post Re: Motor Encoders
Wow, I couldn't find any of those functions in version 3.62 for NXT 2.0. Is there any way you can do the same for NXT 2.0 version 3.62?


Tue Aug 26, 2014 10:26 am
Profile
Rookie

Joined: Fri Sep 20, 2013 8:39 am
Posts: 38
Post Re: Motor Encoders
I have not used any special functions from EV3 and 4.25. There are similar functions in NXT 3.62 for all the functions I used (I think). Try to see if you can guess what 3.62 functions you need to use.
Ignore the displayBigTextLine or use something like (capitalisation to be confirmed) nxtdisplayBigTextLine
delete the reference to setMotorBrakeMode(MasterDriver,motorCoast) => not sure how to do this for individual motors in 3.62 or use bFloatDuringInactiveMotorPWM(True)
resetMotorEncoder(SlaveDrive)=>nMotorEncoder(SlaveDrive)=0
getMotorEncoder(MasterDrive)=>nMotorEncoder[motorA];
setMotorTarget(SlaveDrive,MasterEncoder,100)=>nMotorEncoderTarget[SlaveDrive] plus you need to set the speed of the SlaveDrive

Give it a go and try to see if you can get it to work.

.


Tue Aug 26, 2014 10:51 am
Profile
Rookie

Joined: Mon Aug 25, 2014 2:10 am
Posts: 5
Post Re: Motor Encoders
It worked perfectly for a clockwise motion, but it obviously wouldn't work for an anticlockwise motion considering the motor power is always a positive value. How do you suppose I would make the slave go backward as the master turns backward.
Code:
task main()
{
int Master;
int Slave;
nMotorEncoder[motorA]=0;
nMotorEncoder[motorB]=0;
bFloatDuringInactiveMotorPWM=true;
motor[motorA]=0;
bFloatDuringInactiveMotorPWM=false;
motor[motorB]=0;
eraseDisplay();
   while(true)
   {
      Master=nMotorEncoder[motorA];
      Slave=nMotorEncoder[motorB];
      nxtDisplayString(2,"Master %d", Master);
      nxtDisplayString(5,"Slave %d", Slave);
      
      
      
         while(abs(Master-Slave)>10)
         {
            
            nxtDisplayString(2,"Master %d", Master);
            nxtDisplayString(5,"Slave %d", Slave);
            nMotorEncoderTarget[motorB]=Master;
            motor[motorB]=40;
            wait1Msec(1);
            Master=nMotorEncoder[motorA];
            Slave=nMotorEncoder[motorB];
         
         }         
         motor[motorB]=0;
            
      }
}


Sat Aug 30, 2014 2:06 pm
Profile
Rookie

Joined: Fri Sep 20, 2013 8:39 am
Posts: 38
Post Re: Motor Encoders
I thought the basic program that I posted worked irrespective of the direction the Master moved in on the EV3.

I tried your NXT program and had the problem you described, so I changed the line of your code where you turn on the motor to the following
Code:
 motor[motorB]=40*sgn(Master-Slave);


This fixes the problem.


Sat Aug 30, 2014 7:40 pm
Profile
Rookie

Joined: Mon Aug 25, 2014 2:10 am
Posts: 5
Post Re: Motor Encoders
Haha that worked perfectly thank you, but just out of curiosity, could I know what sgn does?


Sat Aug 30, 2014 11:25 pm
Profile
Rookie

Joined: Fri Sep 20, 2013 8:39 am
Posts: 38
Post Re: Motor Encoders
The sgn function returns the sign of the calculation. So if Master>Slave then it returns positive and does not modify the sign of the desired motor speed. If Master<Slave and the motor needs to turn backwards, the motor speed is set to negative.


Sun Aug 31, 2014 1:38 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 11 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.