Mindsensors MMX motor control

Discussion specific to the intelligent brick, sensors, motors, and more.
Post Reply
nxtdoor
Posts: 1
Joined: 27 Mar 2013, 01:31

Mindsensors MMX motor control

Post by nxtdoor »

I am having trouble running in speed control using the Mindsensors MMX. Basically I want to oscilate the motor back and forth at low speed (i.e. 10) while scanning a light sensor to detect an edge. I am finding that when I reverse speed, the motor will jump approximately 10 to 20 degrees before settling to the desired speed. I have tried using the ramp function (ctrl |= MMX_CONTROL_RAMP ;) of the multiplexer, but it does not seem to help. I have tried a lot of different options including setting speed to zero and using float instead of brake to stop the motor but nothing seems to work. BTW I tried the same code using direct control from the brick and the motors moved smoothly.

When I read the status register, I periodically see that bits 0 and 3 are both set indicating that the motor is operating in both position and speed control. Does this make sense?

My code (for two motors acting like windshield wipers) is as follows:

Code: Select all

#include "NXTMMX-lib.nxc"

// Device Address
#define DIST_ADDR 0x02
#define SensorPort S4
#define SENSORTHRESHOLD 30
#define TAPETHRESHOLD 52
#define LOWSPEED 55
#define MEDSPEED 70
#define HIGHSPEED 81
#define MAXSPEED 100

#define CW  1
#define CCW 2

const byte Addr = 0x06;

task main()           // main task:
{
  int v;
  long lir,lr;      // left initial rotation, left rotation
  long lang;        // left angle
  string msg;
  long a,b,x,c,d;
  bool leftll, leftul, rightll, rightul ;
  int dir ;
  float ang ;
  float y ;
  byte m1, m2 ;

  // Initiialization code
  MMX_Init( SensorPort, Addr, 0 );


   

  
  m1 = MMX_MotorStatus(SensorPort, Addr, MMX_Motor_1) ;
  m2 = MMX_MotorStatus(SensorPort, Addr, MMX_Motor_2) ;
  NumOut(1, LCD_LINE1, m1, 0);
  NumOut(20, LCD_LINE1, m1, 0);


  MMX_Run_Degrees (SensorPort, Addr, MMX_Motor_1, MMX_Direction_Reverse,
                   10, 130, MMX_Completion_Wait_For, MMX_Next_Action_Brake);
  MMX_Run_Degrees (SensorPort, Addr, MMX_Motor_2, MMX_Direction_Reverse,
                   10, 200, MMX_Completion_Wait_For, MMX_Next_Action_Brake);

  m1 = MMX_MotorStatus(SensorPort, Addr, MMX_Motor_1) ;
  m2 = MMX_MotorStatus(SensorPort, Addr, MMX_Motor_2) ;
  NumOut(1, LCD_LINE2, m1, 0);
  NumOut(20, LCD_LINE2, m2, 0);

  MMX_Run_Unlimited( SensorPort, Addr, MMX_Motor_2,MMX_Direction_Forward, 10);
  MMX_Run_Unlimited( SensorPort, Addr, MMX_Motor_1,MMX_Direction_Reverse, 10);
  dir = CCW ;

  m1 = MMX_MotorStatus(SensorPort, Addr, MMX_Motor_1) ;
  m2 = MMX_MotorStatus(SensorPort, Addr, MMX_Motor_2) ;
  NumOut(1, LCD_LINE3, m1, 0);
  NumOut(20, LCD_LINE3, m2, 0);

  Wait(200) ;
  while(true)
  {
     a = -MMX_ReadTachometerPosition(SensorPort, Addr, MMX_Motor_1);
     b = -MMX_ReadTachometerPosition(SensorPort, Addr, MMX_Motor_2);
     if (a > 200 && dir == CCW) {
       MMX_Stop(SensorPort, Addr, MMX_Motor_1, MMX_Next_Action_Float);
       rightul = true ;
     }
     if (a < 130 && dir == CW) {
       MMX_Stop(SensorPort, Addr, MMX_Motor_1, MMX_Next_Action_Float);
       rightll = true ;
     }
     if (b > 200 && dir == CW) {
       MMX_Stop(SensorPort, Addr, MMX_Motor_2, MMX_Next_Action_Float);
       leftul = true ;
     }
     if (b < 130 && dir == CCW) {
       MMX_Stop(SensorPort, Addr, MMX_Motor_2, MMX_Next_Action_Float);
       leftll = true ;
     }

     if (rightul && leftll)
      {
        MMX_Run_Unlimited( SensorPort, Addr, MMX_Motor_1,MMX_Direction_Forward, 0);
        MMX_Run_Unlimited( SensorPort, Addr, MMX_Motor_2,MMX_Direction_Reverse, 0);
        Wait(300) ;
        MMX_Run_Unlimited( SensorPort, Addr, MMX_Motor_1,MMX_Direction_Forward, 10);
        MMX_Run_Unlimited( SensorPort, Addr, MMX_Motor_2,MMX_Direction_Reverse, 10);
        m1 = MMX_MotorStatus(SensorPort, Addr, MMX_Motor_1) ;
        m2 = MMX_MotorStatus(SensorPort, Addr, MMX_Motor_2) ;
        NumOut(1, LCD_LINE4, m1, 0);
        NumOut(20, LCD_LINE4, m2, 0);
         Wait(200) ;
        m1 = MMX_MotorStatus(SensorPort, Addr, MMX_Motor_1) ;
        m2 = MMX_MotorStatus(SensorPort, Addr, MMX_Motor_2) ;
        NumOut(1, LCD_LINE5, m1, 0);
        NumOut(20, LCD_LINE5, m2, 0);

        rightul = leftll = false ;
        dir = CW ;
      }
      if (rightll && leftul)
      {
        MMX_Run_Unlimited( SensorPort, Addr, MMX_Motor_1,MMX_Direction_Reverse, 0);
        MMX_Run_Unlimited( SensorPort, Addr, MMX_Motor_2,MMX_Direction_Forward, 0);
        Wait(300) ;
        MMX_Run_Unlimited( SensorPort, Addr, MMX_Motor_1,MMX_Direction_Reverse, 10);
        MMX_Run_Unlimited( SensorPort, Addr, MMX_Motor_2,MMX_Direction_Forward, 10);
        m1 = MMX_MotorStatus(SensorPort, Addr, MMX_Motor_1) ;
        m2 = MMX_MotorStatus(SensorPort, Addr, MMX_Motor_2) ;
        NumOut(1, LCD_LINE4, m1, 0);
        NumOut(20, LCD_LINE4, m2, 0);
         Wait(200);
        m1 = MMX_MotorStatus(SensorPort, Addr, MMX_Motor_1) ;
        m2 = MMX_MotorStatus(SensorPort, Addr, MMX_Motor_2) ;
        NumOut(1, LCD_LINE5, m1, 0);
        NumOut(20, LCD_LINE5, m2, 0);
        rightll = leftul = false ;
        dir = CCW ;

      }
  }


}
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest