Trying to organize 4 serial commands in 10 mS with machine State, but....

Status
Not open for further replies.

laptophead

Well-known member
Overview
I run a robot with 8 motors connected to UARTs and RS485 convertors.
I devoted 4 serial channels, 2 motors each.

When I send a serial command, I need to wait about 2-3ms for the response before I can send the next one.

I would like to have a timer system that runs 100 times per sec and reads the encoders non stop, and is able to send micro steps to the motors in the same 10mS span.

The 10ms allocation:
0-2 ms Read first encoder group of 4
2-4 ms Read second encoder group
4-7 ms Send motion command Motor group 1 ( these need 3ms , they have angle/speed data) //
7-10 ms Send same to Motor group 2


So I made a 10 ms stamp timer in the loop
if (millis() - ReadEncMillis >= 10)
{ Stamp = millis();
// Serial.println (Stamp);
ReadEncMillis = millis();
}

and then I wrote a little machine state function that is stuck in the first state

HTML:
void MasterTimer ()

{
  if ( millis() - Stamp < 2)
  {
    C_States =  Enc_Gr_1;
    Serial.println ("Enc_Gr_1");
    return;
  }
  else if ( millis() - Stamp >= 2 and  millis() - Stamp < 4)
  { C_States =  Enc_Gr_2;
    Serial.println ("Enc_Gr_2");
  return;
  }

  else if ( millis() - Stamp >= 4 and  millis() - Stamp < 7)
  { C_States =  Mot_Gr_1;
   Serial.println ("Mot_Gr_1");
  return;
  }

  else if ( millis() - Stamp >= 7 and millis() - Stamp <10)
  { C_States =  Mot_Gr_2;
  Serial.println ("Mot_Gr_2");
  return;
  }

 // Serial.println (C_States);
 /* 
  switch (C_States)

  {
    case Enc_Gr_1:  // reading first Group of encoders
      Read_Enc (1);  //A on serial 2
      Read_Enc (12); //B-ser 3
      Read_Enc (11); //C-ser1
      Read_Enc (5);  //D-Ser4
      break;
      // 2mS later:
      case Enc_Gr_2:  // reading second group of encoders
    // Read_Enc (2);  // A on Serial 2
    //  Read_Enc (3); // C on Serial 1
     // Read_Enc (6); // D on Serial 4
    //  Read_Enc (13);// B on Serial 3
      break;
  
      ///next I was thinking to write motor commands group one and 2.
  }

  */

}

I know this is demanding, so I am using the teensy 4.1

Is there a smarter way to do this?

Thanks
 
Status
Not open for further replies.
Back
Top