Page 1 of 1

NXC: MS RCX motor mux (MTRMX-Nx): easier commands to control

Posted: 29 Mar 2013, 11:42
by HaWe
hey,
using the Mindsensors MS RCX motor mux (MTRMX-Nx): here are some easier commands to control it:

Code: Select all

safecall void OUT_MSMMux(char NXTport, byte muxmot, int speed)
you just have to pass NXTport, the motor# of the muxport, and the speed in a similar way you are used to for NXT motors, i.e. in a range from -100 to +100.
0 and values >=1000 are features to switch the motor with those commands either to coast or to break, so
speed: -100...+100,
0: coast,
>=1000: break,
positive values: forward
negative values: reverse

By this you are able - if you wish - to write your own functional wraps around this function :

Code: Select all

void OFF_MSMMux()   // pass speed=1000 to OUT_MSMMux
void COAST_MSMMux() // pass speed=0 to OUT_MSMMux
mux motors may have the numbers 1...4

here's a demonstration code :

Code: Select all

#define  RCXMMux_Port S2


#define MC_FLOAT      0x00
#define MC_COAST      0x00 // redundant
#define MC_FORWARD    0x01
#define MC_REVERSE    0x02
#define MC_BRAKE      0x03

#define RCXMMux_ADDR  0xB4

/******************************************************************************/
inline void MS_MTRMXControl(byte port, byte i2cAddr, byte motorNumber, byte control, byte speed)
/******************************************************************************/
{
	byte location;
  byte message[20];
  byte nByteReady = 0;

  location = 0x40 + motorNumber*2;
  ArrayBuild(message, i2cAddr, location, control, speed);
  while (I2CStatus(RCXMMux_Port, nByteReady) ==  STAT_COMM_PENDING);
  I2CWrite(port, 0, message);
  while (I2CStatus(RCXMMux_Port, nByteReady) ==  STAT_COMM_PENDING);

}

/******************************************************************************/
// OUT_MSMMuxspeed= -100...0...+100 : 0=coast, 1000=break
// muxmot= 1...4
inline void OUT_MSMMux(char NXTport, byte muxmot, int speed)
/******************************************************************************/
{
  byte motcmd=0;

  if (speed>=1000) { motcmd= MC_BRAKE; speed=255; } // <=== special brake value
  else {

    if (speed>0) { motcmd= MC_FORWARD; speed= speed*255/100; }
    else
    if (speed<0) { motcmd= MC_REVERSE; speed=-speed*255/100; }
    else
    if (speed==0) { motcmd= MC_COAST; speed=0; }

    if(speed>255) speed=255;
  }

  MS_MTRMXControl(NXTport, RCXMMux_ADDR, muxmot, motcmd, speed);

}





task main() {
  TextOut(0,56, "RCXMMux Test");
  SetSensorLowspeed(RCXMMux_Port);

  while(1) {
  
     if ( ButtonPressed(BTNLEFT, true) ) {
       OUT_MSMMux(RCXMMux_Port,  1,  100);
     }
     
     if ( ButtonPressed(BTNRIGHT, true) ) {
       OUT_MSMMux(RCXMMux_Port,  1, -100);
     }
     
     if ( ButtonPressed(BTNCENTER, true) ) {
       OUT_MSMMux(RCXMMux_Port,  1,  0);
     }
  
  }

}
share and enjoy!