USB Serial - Fast response (algorithm idea)

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Hello everyone,

I'm new to the world of embedded (some Arduino experience) and I've scratch my head on a problem for several weeks without any improvements. I also have more experience in the world of FPGA (and maybe my ideas are not ideal for embedded due to that, I always try to do everything in parallel) . I'm coming to you in the hope you could help me.

I'm using a PSOC (FreeSoC2 Development Board - PSoC5LP - DEV-13714 - SparkFun Electronics ) which update a lot a values from different ADC and other modules (via software serial). Updating all the values takes ~500ms. I also have a GUI program in python to show the status of the different modules and to do some basic control. I'm updating the values for monitoring by sending a command via USBUART and waiting for the response.

Right now, the algorithm looks like that (pseudo-code)

-------------------------------------

while(1)

{

if (0u != USBUART_DataIsReady())

     {

          /* Read received data and re-enable OUT endpoint. */

          count = USBUART_GetAll(buffer);

          if (0u != count)

          {

               -> parse data and send the information to the PC (example, if I send 'rt1\r\n', send back the temperature of the 1st device)

          }

     }

if (flag_timer_interrupt) //interrupt each 1.5sec

{

     update_all_values(); // this command takes ~500ms

     flag_timer_interrupt = 0;

}

}

-------------------------------------

I'm looking for a way to have the PSOC respond "immediately" to the computer because you can imagine the delay if I ask for data while it's updating the values.

I've tough about parsing and sending the data in an interrupt, but I also read it was not recommended. I also had an idea to an algorithm like that but it doesn't look efficient/beautiful in my opinion:

while(1)

{

     update_one_value();

     check_and_parse_usb();

}

Do you have any idea if either it is possible to run the function in parallel or what I could try to implement to have a more efficient algorithm.

Thank you for your time,

Alex

0 Likes
1 Solution

Alex,

This is realm of asyncronous communication: after sending a request the can be a (I) valid response, (ii) delayed response,(iii) no response timeout, (iv) error message, (v) delayed response from another sensor. You will end up writing multithreaded state machine on receiving side. Things complicate further if accurate timestamp is required along the data, e.g. GPS coordinates of the flying quadcopter + timestamp, because of inherent delay of the request-sample-feedback.

I would send each individual data as soon as it is available. Maybe it is possible to separate your sensors into fast domain (e.g. ADC) and slow ones (temperature sensors).

In general, it is not advisable to have PSoC stuck in single procedure for 500ms, it is better to separate it into small tasks.

          update_all_values(); // this command takes ~500ms

View solution in original post

0 Likes
4 Replies
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

alex,

Instead of waiting for a request for data I would send results as soon as data collected (sort of a beacon).

while(1)

{

     if (flag_timer_interrupt) //interrupt each 1.5sec

     {

          flag_timer_interrupt = 0;

          update_all_values(); // this command takes ~500ms

          check_and_parse_usb();

     }

}

It is easier to sort data on the receiver part in PC with unlimited resources.

/odissey1

0 Likes
Anonymous
Not applicable

Hello odissey1,

I like this idea, simple and elegant.

I will implement it like that, but will keep a function to read the serial port outside the function triggered by the flag_timer_interrupt to send data from the computer to the PSOC.

Thank you for your help.

PS: if anyone still have an idea for my first idea to work (if it's possible), I would like to know by curiosity (and for future project). Thanks

0 Likes

Alex,

This is realm of asyncronous communication: after sending a request the can be a (I) valid response, (ii) delayed response,(iii) no response timeout, (iv) error message, (v) delayed response from another sensor. You will end up writing multithreaded state machine on receiving side. Things complicate further if accurate timestamp is required along the data, e.g. GPS coordinates of the flying quadcopter + timestamp, because of inherent delay of the request-sample-feedback.

I would send each individual data as soon as it is available. Maybe it is possible to separate your sensors into fast domain (e.g. ADC) and slow ones (temperature sensors).

In general, it is not advisable to have PSoC stuck in single procedure for 500ms, it is better to separate it into small tasks.

          update_all_values(); // this command takes ~500ms

0 Likes
Anonymous
Not applicable

Communications looks like an interesting subject, but so it's so complex. I'll read on the asynchronous comms subject, thanks for the info.

As time stamp is not very important in my case, I'll go the solution of sending the data as soon as they are ready. And yeah, separating update_all_values(); into smaller task is a very good idea.

Thank you for your help.

Alex

0 Likes