Xenon - UART and Performance

I have not used the synchronization output as I recall the sensor’s technical support saying something about not using it.

As to the analog out, they also recommended I not use the analog output due to some problems I had (with a PC though).

I will get a Photon and test with it and report.

Thanks again.

Also, if you set the device to 200 samples/sec, each sample will be presented at 5ms intervals. At 115Kbaud, two contiguous bytes will take about 200us to transmit, leaving you with a 4800us interval between values. @bko can check me here but if you synchronized the “start of data” once, then every two bytes in the receive buffer will represent a new value. Unless you overflow the receive buffer, this will hold true forever unless the sensor is powered off or goes to a non-transmitting state. It just seems to me your approach here is incorrect as @bko pointed out.

2 Likes

OK, I have done a new test where everything is the same and EXACT code (below), breadboard and all.

With a Photon, Laser Frequency: 500 Hz.

With a Xenon, Laser Frequency: 15 Hz.

This is also confirmed by the lethargic distance change when I aim the sensor at different objects with the Xenon. With the Photon, the distance change is displayed immediately …

It is clear something is wrong. Could it be related to the Mesh?

SYSTEM_THREAD(ENABLED);

//=======================================================================================================
String verNum = "1.00";
//=======================================================================================================
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

//=======================================================================================================
int strength;
int distance;
int distanceFT;

int len;
char buf[60];

int myCase = 0;

int hertz = 0;
//=======================================================================================================

void setup() {
  //=================================================================
  Serial1.begin(38400);      //on SoftSerial - Sensor cannot support any faster than baud 38400
  delay(2000);
   //=================================================================   
long start = millis();
long end;

for (int i=0; i <= 1000; i++){
      readLightware();
      delay(1);
   }
 
   end = millis();
   hertz = 1000 / ((end - start) / 1000) ;
   
   
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)

    display.clearDisplay();   // clears the screen and buffer
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(0, 0);
    display.println("Trigger");
    display.setCursor(0, 25);
    //display.println("Ver. " + verNum);
    display.println(hertz);
    display.setCursor(0, 50);
    display.println("Jimmie");
    display.display();
    delay(5000);
  //=================================================================
    //------------------------------------------------------------------
  
}

void loop() {
  readLightware();
  showResults();
  delay(1);
}


//===============================================================================================================

void showResults()
{
  display.clearDisplay();
  display.setCursor(0, 0);
  display.println("Dist ");
  display.setCursor(60, 0);
  display.println(distance);
  display.setCursor(0, 40);
  display.println("Freq ");
  display.setCursor(60, 40);
  //display.println(myLight);
  display.println(hertz);
  display.display();


  delay(1);
}

//===============================================================================================================
//Laser
//===============================================================================================================
void readLightware()
{
  //while (LightW.available() < 2);
  while (Serial1.available())
    //if (LightW.available() > 2);

    Serial1.read();

  while (Serial1.available() == 0);
  unsigned long t0 = micros();
  while (Serial1.available() == 1);
  unsigned long t1 = micros();
  while (Serial1.available() == 2);
  unsigned long t2 = micros();

  int byte0 = Serial1.read();
  int byte1 = Serial1.read();
  int byte2 = Serial1.read();

  float distanceM = 0.0;

  if ((t1 - t0) < (t2 - t1))
  {
    distanceM = byte1 + byte0 / 256.0;
    myCase = 0;
  }
  else
  {
    distanceM = byte2 + byte1 / 256.0;
    myCase = 1;
  }
  
  //Serial1.flush();
  //---------------------------------------------
  distance = distanceM * 100;   //convert to cm!
  distanceFT = distance * 0.033;
  //---------------------------------------------
}

SF_Photon|375x500

Hello @bko:

I realize that code working on one micro may not work on another however I am not sure why you say above that it is not desirable? I am not a computer scientist, but it would seem to me that using a relatively accurate internal timer should work. It has worked before on an Arduino UNO, Due, Photon and Electron. If it is not working on a Xenon, it would seem to be a fault of that architecture?

Also, even if what you are saying is correct, this should give wrong distance results if the bytes are misread, but should not affect the read frequency.

I wish to thank @bko, @peekay123 and @ninjatill for taking the time to help and the advice they gave.

I had also changed the code to test @bko point about the code being inefficient, and sure enough, when the byte ordering code was taken out, it appears that the Xenon UART worked fine.

I am still not convinced that code that works on an UNO, DUE, Teensy, Photon and ESP32 would not work on a Xenon but have decided to just use another sensor.

1 Like

@Jimmie, I was having the similar results on another application I was working on. I found that display.display() in your showResults() function takes around 70ms to complete, that’s roughly 13-14 Hz. Remove the display and send you data out serially and I’m sure you’ll get the update frequency your looking for.

I have only used that display on a Xenon so far.

1 Like

@Jseiler, thank you. Yes, as you mentioned, the display can delay things but the test above was done prior to writing to the screen by cycling the readout 1000 times, so the screen has no impact in this case.