Reduce Response Rates

Hi All,

I've been doing investigations on how to reduce my response rates in my project and essentially what I'm finding is, "It depends on the project".

So far I've managed to cut response rate in about half by making these two changes:

  1. Change from a series of iterative if statements to an array of structs that can take an int(index) and give all the corresponding struct as output.
  2. Change from Serial.print commands to Serial.write (sending bytes instead of strings).

My robot is taking about 0.75 seconds to physically adjust after I click a button in VS environment. So I'm going to list out my steps and hope you all could point to changes I can make to reduce the response rate. :slight_smile:

Platform (PC/Visual Studio program sending code to an ATMega2560 to drive 4 motors (wheels on a robot) via a USB type A to type B connector (COM port)).
PC specs, i7-10750H (2.6 GHz), 16GB RAM and Geforce RTX 3060 GPU
Visual Studio 2022, .NET Framework
Currently sending at baud rate of 9600 and all other standard SerialPort options.

From PC/VS code:

  1. Button_Click event
  2. Send 1 byte over SerialPort.Write function
  3. Wait until response byte back=1 before allowing PC to send another command (so I don't miss commands because arduino is taking too long to respond)

Arduino code (I have some other functions that print details over Serial.print for debugging purposes that I haven't included here (they are separate function calls). I remove all of these calls/functions when monitoring response rates. I'm just not sure if extra functions that aren't being used may be causing delays. Same goes for comments... do extra comments cause issues???):

#include "MeMegaPi.h"

//Front Right Motor
MeMegaPiDCMotor motor2(PORT1A);

//Back Right Motor
MeMegaPiDCMotor motor4(PORT1B);

//Front Left Motor
MeMegaPiDCMotor motor1(PORT2A);

//Back Left Motor
MeMegaPiDCMotor motor3(PORT2B);

//Declare a struct to contain Directions
struct MotorDirection{
  int m1;
  int m2;
  int m3;
  int m4;
};

//Define direction matrices
MotorDirection stop = {0,0,0,0};
MotorDirection forward = {1,1,1,1};
MotorDirection forwardleft = {0,1,1,0};
MotorDirection left = {-1,1,1,-1};

//Populate array with directions and define it's size for out of bounds capture later
MotorDirection Directions[] {stop,forward,forwardleft,left};
const int DirSize = sizeof(Directions)/sizeof(Directions[0])-1;

const int bufSize = 5;
byte message[bufSize];
int index = 0;

// Define motor speeds, will be adjusted with additional indexes
int motorSpeed = 80;

void setup()
{
  Serial.begin(9600);
  Serial.write(1);
}

void loop()
{
  // If something exists, read the bytes from the serial port
  if (Serial.available() > 0){
    Serial.readBytes(message,bufSize);
    index = message[0];
    if (index <= DirSize)
    {
      ChangeDirection(Directions[index], 80,false);
    }
    else
    {
      ChangeDirection(Directions[0], 80,false);
      Serial.print("The index was out of bounds");
    }
    Serial.write(1);
  }
}

/* 
Stop on 0 command. Robot will move straight forward with 1... details to be defined.
Speed commands ... to be defined. speed value: between -255 and 255. 
Unknown cmd will stop robot.
*/
void ChangeDirection(MotorDirection md, int speed)
{
  motor1.run(md.m1*speed); 
  motor2.run(md.m2*speed); 
  motor3.run(md.m3*speed);
  motor4.run(md.m4*speed);
}

They won't because the compiler will remove unused functions from the code

No. There are no comments in the compiled code

Also if anyone has tips on how to properly monitor timings, etc I would appreciate it. That would at least help me breakdown where the time is being consumed (VS side, comms handoff, or arduino side).

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.