Pololu Robotics & Electronics
Menu
My account Comments or questions? About Pololu Contact Ordering information Distributors

Pololu Forum

Tic 249 Multi Serial Position Example Codes

Hi,

I tried to modify Multi Serial Speed codes example to Multi Serial Position control,
but once uploaded the motors doesn’t move. Please can you help me to check flaws in my modifications:

#include <Tic.h>

// On boards with a hardware serial port available for use, use
// that port to communicate with the Tic. For other boards,
// create a SoftwareSerial object using pin 10 to receive (RX)
// and pin 11 to transmit (TX).
#ifdef SERIAL_PORT_HARDWARE_OPEN
#define ticSerial SERIAL_PORT_HARDWARE_OPEN
#else
#include <SoftwareSerial.h>
SoftwareSerial ticSerial(10, 11);
#endif

TicSerial tic1(ticSerial, 14);
TicSerial tic2(ticSerial, 15);


void setup()
{
  // Set the baud rate.
  ticSerial.begin(9600);

  // Give the Tic some time to start up.
  delay(20);

  // Set the Tic's current position to 0, so that when we command
  // it to move later, it will move a predictable amount.
  tic1.haltAndSetPosition(0);
  tic2.haltAndSetPosition(0);
  // Tells the Tic that it is OK to start driving the motor.  The
  // Tic's safe-start feature helps avoid unexpected, accidental
  // movement of the motor: if an error happens, the Tic will not
  // drive the motor again until it receives the Exit Safe Start
  // command.  The safe-start feature can be disbled in the Tic
  // Control Center.
  tic1.exitSafeStart();
  tic2.exitSafeStart();
}

// Sends a "Reset command timeout" command to the Tic.  We must
// call this at least once per second, or else a command timeout
// error will happen.  The Tic's default command timeout period
// is 1000 ms, but it can be changed or disabled in the Tic
// Control Center.
void resetCommandTimeout()
{
  tic1.resetCommandTimeout();
  tic2.resetCommandTimeout();
}

// Delays for the specified number of milliseconds while
// resetting the Tic's command timeout so that its movement does
// not get interrupted by errors.
void delayWhileResettingCommandTimeout(uint32_t ms)
{
  uint32_t start = millis();
  do
  {
    resetCommandTimeout();
  } while ((uint32_t)(millis() - start) <= ms);
}

// Polls the Tic, waiting for it to reach the specified target
// position.  Note that if the Tic detects an error, the Tic will
// probably go into safe-start mode and never reach its target
// position, so this function will loop infinitely.  If that
// happens, you will need to reset your Arduino.
void waitForPosition(int32_t targetPosition)
{
  do
  {
    resetCommandTimeout();
  } while (tic1.getCurrentPosition() != targetPosition);
    while (tic2.getCurrentPosition() != targetPosition);
}

void loop()
{
  // Tell the Tic to move to position 100, and wait until it gets
  // there.
  tic1.setTargetPosition(100);
  waitForPosition(100);
  tic2.setTargetPosition(-100);
  waitForPosition(100);


  // Tell the Tic to move to position -100, and delay for 3000 ms
  // to give it time to get there.
  tic1.setTargetPosition(-100);
  tic2.setTargetPosition(-100);
  delayWhileResettingCommandTimeout(3000);
}

Hello.

I moved your post to the “Motor controllers/drivers and motor” section of the forum since it seemed more appropriate, and edited your post to include code tags to keep it easily readable.

There are a couple problems with your waitForPosition() function. The first problem I see is that you are passing it a single position to wait for, and trying to wait for both Tics to reach that same position, but that is not how you are using it in your code since you are sending tic1 and tic2 to different positions.

Secondly, in your current waitForPosition() code, you have a do…while structure checking tic1 followed by a normal while loop for tic2. So, even if it successfully waits for tic1 to reach the target position, you will probably get a command timeout error if tic2 takes longer than the configured timeout duration to reach the target.

One way to solve this would be to create separate functions for checking tic1 and tic2. Another option would be keeping track of the tic1 and tic2 positions in separate variables, so you could check them both in the same function. Alternatively, you could pass the function an argument to tell it which Tic you are trying to wait for.

Brandon