My first little project


#1

Hey guys!

I know this isn’t much, but I thought it was kind of cool. I am using a CMU Pixy camera for object tracking connected through Arduino and then connected to the Odrive using UART. It works as expected but if anyone has any ideas how to reduce the latency that would be awesome! The Arduino code is below:

#include <SoftwareSerial.h>
#include <ODriveArduino.h>
#include <SPI.h>  
#include <Pixy.h>

// Printing with stream operator
template<class T> inline Print& operator <<(Print &obj,     T arg) { obj.print(arg);    return obj; }
template<>        inline Print& operator <<(Print &obj, float arg) { obj.print(arg, 4); return obj; }

// Serial to the ODrive
SoftwareSerial odrive_serial(8, 9); //RX (ODrive TX), TX (ODrive RX)

// ODrive object
ODriveArduino odrive(odrive_serial);

int test = 10;
int ledPin = 5;
int buttonApin = 2;
int buttonBpin = 3;

byte leds = 0;
Pixy pixy;

void setup() {
pixy.init();
  {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonApin, INPUT_PULLUP);  
  pinMode(buttonBpin, INPUT_PULLUP);  
}

  // ODrive uses 115200 baud
  odrive_serial.begin(115200);

  // Serial to PC
  Serial.begin(115200);
  while (!Serial) ; // wait for Arduino Serial Monitor to open

  Serial.println("ODriveArduino alpha.");
  Serial.println("Setting parameters...");

  // In this example we set the same parameters to both motors.
  // You can of course set them different if you want.
  for (int motor = 0; motor < 2; ++motor) {
    odrive.SetParameter(motor, odrive.PARAM_FLOAT_CURRENT_CONTROL_CURRENT_LIM, 10.0f);  // [A]
    odrive.SetParameter(motor, odrive.PARAM_FLOAT_VEL_LIMIT, 15000.0f);                 // [counts/s]
    odrive.SetParameter(motor, odrive.PARAM_FLOAT_POS_GAIN, 20.0f);                     // [(counts/s) / counts]
    odrive.SetParameter(motor, odrive.PARAM_FLOAT_VEL_GAIN, 20.0f/10000.0f);            // [A/(counts/s)]
    odrive.SetParameter(motor, odrive.PARAM_FLOAT_VEL_INTEGRATOR_GAIN, 0.0f/10000.0f); // [A/(counts/s * s)]
  }

  Serial.println("Ready!");
  Serial.println("Send the character 's' to exectue test move");
  Serial.println("Send the character 'b' to read bus voltage");
  Serial.println("Send the character 'p' to read motor positions in a 10s loop");
}

void loop() {
  static int i = 0;
  int j;
  uint16_t blocks;
  char buf[32]; 
blocks = pixy.getBlocks();
  
  
  {
  if (digitalRead(buttonApin) == HIGH)
  {
        float pos_m0= (0,pixy.blocks[0].y*-2+1370) ;
        odrive.SetPosition(0,pos_m0);
       
      }
      if (digitalRead(buttonBpin) == LOW)
  {
        float pos_m0= (0,1000) ;
        odrive.SetPosition(0,pos_m0);
       
      }
 
}
}

#2

Are you predicting the location, including your lag, or a are you moving to its last position + lag. If you lag is 100ms, and you predict its location 100ms in the future you should compensate. And you should be able to fake it out :slight_smile:


#3

I am not predicting its location. The odrive is moving to a specific location in relation to the Y cord of the pixy camera. The camera is tracking the object quickly (50fps) I am just trying to determine where the latency is coming from since it seems like a lot more then 1/50th of a second. I haven’t had time to actually time it yet, hopefully tonight.


#4

Hello. Can you please, explain this part of the code in detail? Specially, the lines below?
float pos_m0= (0,pixy.blocks[0].y*-2+1370) ;
odrive.SetPosition(0,pos_m0);

and
float pos_m0= (0,1000) ;
odrive.SetPosition(0,pos_m0);

Thanks very much. I really appreciate any help.


#5

@hbtousa This very much is the blind leading the blind, I am not a software dev I just figure things out through trial and error so keep that in mind with everything I say.

The first question would be, are you trying to use a pixy camera? If the answer is no then we don’t need to go any further. If yes then the simple answer is, I mashed together the “hello world” from pixy and one of the test programs from odrive to get them to work together.

I’ll go into more detail if you would like, it has been a few months so it will take me a little bit to remember everything.


#6

Thanks very much for your reply. I was just trying to understand the motor’s positioning part but I have a better understanding now than shortly before I asked you the question.