Page 2 of 2

Re: RS485 NXT multiplexer

Posted: 14 Mar 2012, 18:14
by mattallen37
The first line scales the ly value (from the PSP-Nx) from the number range of 0 through 255 to -100 through 100 (to be fed directly into the motor PWM control).

The other two lines actually set the values, which will get updated on the slave during the next communication transaction (which is automatic).

So far it doesn't support setting values for multiple motors at once, but I'll probably add that sometime. Once I do, you will be able to do something like:

Code: Select all

MuxOnFwd(RS485Mux1, OUT_BC, speed);

Re: RS485 NXT multiplexer

Posted: 14 Mar 2012, 18:47
by HaWe
I don't have a PSPRemote unfortunately, so I have to test by another code...

if I want to command:

Code: Select all

inline void MotorOff(const byte &port) {
   SetOutput(port, Power, 0,
             OutputMode,  OUT_MODE_COAST,
             RegMode,     OUT_REGMODE_IDLE,
             RunState,    OUT_RUNSTATE_IDLE,
             UpdateFlags, UF_UPDATE_MODE + UF_UPDATE_SPEED);
}

char  Mux1A_CmdRdy;  // Semaphore to indicate if following task is finished or if it's running

// MuxOUT_A_md drives mux motor until the medium touch sensor is pressed
// Mux1_SensorValue(S3): claw up position
// Mux1_SensorValue(S2): claw medium position
// Mux1_SensorValue(S1): claw down position
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
task MuxOUT_A_md() { 
  char speed ;
  speed=100;
  Mux1A_CmdRdy=false;

  MotorOff(Mux1_OUT_A); // breaks motor actions which have been started by different tasks before and stops
  Wait(1);
  while(!(Mux1_SensorValue(S2))) {
      if(Mux1_SensorValue(S1)) speed=-100;
      Mux1_OnFwd(Mux1_OUT_A, speed);
      Wait(1);
      if (Mux1_SensorValue(S2)) {  // in case down pos = S1 pressed
        Mux1_MotorOff(Mux1_OUT_A); // then turn reverse
        speed=0;
        Wait(50);
      }
  }
  Mux1_Coast(Mux1_OUT_A);
  Mux1A_CmdRdy=true;
  Wait(1);
}
how would be the correct syntax to do it with your lib?

Re: RS485 NXT multiplexer

Posted: 14 Mar 2012, 19:14
by mattallen37
I must say, I have no idea what you're trying to do here, or what you're asking. Do you want an equivalent to your MotorOff function, but over the network? How does your function compare to using Coast or CoastEx?

Also, why do you reference to the port variable? Why not use "const byte port" or "byte port"?

Re: RS485 NXT multiplexer

Posted: 14 Mar 2012, 19:48
by HaWe
I currently don't understand your remote commands, I am just trying to translate my code for a NXT which controls local IOs into a code which will control remote IOs on a remote NXT.
So the task still runs locally on the master, but the sensors and motors are attached to a remote NXT (slave).

The original "local IO" code which has to be translated into a "remote IO version" is this one:

Code: Select all

inline void MotorOff(const byte &port)
{
   SetOutput(port, Power, 0,
             OutputMode,  OUT_MODE_COAST,
             RegMode,     OUT_REGMODE_IDLE,
             RunState,    OUT_RUNSTATE_IDLE,
             UpdateFlags, UF_UPDATE_MODE + UF_UPDATE_SPEED);
}



char CmdRdy;

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
task OUT_A_md() {
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  char speed ;
  speed=100;
  CmdRdy=false;

  MotorOff(OUT_A);
  Wait(1);
  while(!(SensorValue(S2))) {
      if(SensorValue(S1)) speed=-100;
      OnFwd(OUT_A, speed);
      Wait(1);
      if (SensorValue(S2)) {  // dn pos = S1
        MotorOff(OUT_A);
        speed=0;
        Wait(50);
      }
  }
  Coast(OUT_A);
  CmdRdy=true;
  Wait(1);
}

Re: RS485 NXT multiplexer

Posted: 14 Mar 2012, 20:41
by mattallen37
doc-helmut wrote:I currently don't understand your remote commands, I am just trying to translate my code for a NXT which controls local IOs into a code which will control remote IOs on a remote NXT.
So the task still runf locally on the master, but the sensors and motors are attached to a remote NXT (slave).
But what does your MotorOff function do? For now, I'll assume it does the same as Coast. I think the following is what you want:

Code: Select all

#include "MyRS485MuxMaster lib 0.02.nxc"

inline void MotorOff(byte slave, byte port)
{
  MuxCoast(slave, port);
}

char CmdRdy;

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
task OUT_A_md() {
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  char speed ;
  speed=100;
  CmdRdy=false;

  MotorOff(RS485MuxSlave1, OUT_A);
  Wait(1);
  while(!(MuxSensor(RS485MuxSlave1, S2))) {
      if(MuxSensor(RS485MuxSlave1, S1)) speed=-100;
      MuxOnFwd(RS485MuxSlave1, OUT_A, speed);
      Wait(1);
      if (MuxSensor(RS485MuxSlave1, S2)) {  // dn pos = S1
        MotorOff(RS485MuxSlave1, OUT_A);
        speed=0;
        Wait(50);
      }
  }
  MuxCoast(RS485MuxSlave1, OUT_A);
  CmdRdy=true;
  Wait(1);
}
However, you will need my latest version (attached) in order to use that without modification. Also remember to setup the sensors on the slave (you can do it remotely using MuxSetSensorType, MuxSetSensorMode, and MuxSetSensor).
RS485 Mux 0.02.zip
(10.63 KiB) Downloaded 154 times
Included in the examples are some first attempts at user-data transmissions. So far sending user-data seems to work fine. In the master example, press the orange button to initiate the transfer (state of three NXT buttons). The master will send it's data (buttons states), and the slave will return it's own data (it's buttons states). This will have an effect on the Mux, but it should only effect the timing (present a slight delay in updates, so that the user-data can be transferred). This is all done automatically, so don't worry about it too much.

I also implemented an 8-bit checksum on all messages, to spot transmission errors (shouldn't ever happen on a wired connection, but you never know). So there are now 4 bytes overhead on all messages.

If an NXT detects an error in the checksum, it will discard the message as if it was sent to a different NXT.

If the slave NXT doesn't receive any valid communication for more than n number of ms (default 500), it will go into a timed out state (float the motors, and clear the active flag).

Re: RS485 NXT multiplexer

Posted: 15 Mar 2012, 08:04
by HaWe
the motor-off function is used by multiple tasks to break motor actions if they have been started by other tasks in order to stop them prematurely.

E.g., lets assume that a task ABC has once started Remote_A to turn 6000 degrees by RotateMotor(Ex) and the motor will be still running yet for a long while,
but now a higher priority task XYZ needs to take control of the motor, it first has to stop the rotatemotor(Ex) action by motorOff and then stops the lower priority task ABC by stop ABC if necessary,
before XYZ itself now can start the motor as intended (e.g., reverse or whatever).

I guess your MuxCoast probably will do the same...

edit:
for the moment it's a little hard to use my robot as a rs485 platform because every sensor slot is in use and I don't have an extra one to use for rs485 - I would need 1 more NXT which I currently don't have for 3 missing S4 ports and I have to rewrite all remote function call to target a different slave as a substitute for master S4, slave1-S4, and slave2-S4.
That would need some time for rewriting the code...