best code organization to read multiple sensors

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

User avatar
Korken
Posts: 270
Joined: Wed Apr 02, 2014 4:09 pm
Has thanked: 5 times
Been thanked: 6 times

Re: best code organization to read multiple sensors

Post by Korken »

I believe this is a good way to go!
Also use 100% static defined for everything. In MCUs RAM errors should be found at compile-time, not when strange things happen because you ran out of RAM. :)

Do you have a link to where you develop your code? I'd like to have a peek!
lesto
Posts: 55
Joined: Thu May 22, 2014 11:27 am

Re: best code organization to read multiple sensors

Post by lesto »

yep, here it is! https://github.com/MauroMombelli/testChibiOsStm32F3 i'm a bit tired today, so i don't think i'll get to code something.

it is alot messy as i'm putting many things in the main, but that's is how i code, when i will like the architecture i'll refractor the function around. Notice that in themain there is still a lot of #IFDEF of older code's implementation (and sensor's code still have to be polished)
User avatar
Korken
Posts: 270
Joined: Wed Apr 02, 2014 4:09 pm
Has thanked: 5 times
Been thanked: 6 times

Re: best code organization to read multiple sensors

Post by Korken »

I see how you want it to work.
I will write a version using parts your design to show how I would have done it. I think you are making it a bit overly complex than it needs to be.

Give me a day and I will link it. :)
User avatar
Korken
Posts: 270
Joined: Wed Apr 02, 2014 4:09 pm
Has thanked: 5 times
Been thanked: 6 times

Re: best code organization to read multiple sensors

Post by Korken »

Hi,

This is how I would do it. Perhaps there are better ways, but it small and easy to follow.
Give it a look and tell me what you think: https://github.com/korken89/EmbeddedGenericSensor code is in modules/sensor_read folder.
I have not populated the main.c yet., I'm working on an example. :)

I expanded on your definitions. For example I do not make the driver initialize the EXTConfig, I leave this to the user.
Instead there is a generic "ext"-callback that shall be used for sensor interrupts.
Also I added so, if it is important, that the rounding errors in the polled time does not accumul'ate over time.
And I added a function set to "inject" sensor reads. That is sensor that are not interrupt driven or polled.

I have tested parts of it, but not all yet. However the code is well comented so you should have no problems understanding it.

Please come with feedback and ideas. :)

Edit: I also added a way to include parameters to the sensor_read and sensor_init functions.
Such as a pointer to the sensor object.
lesto
Posts: 55
Joined: Thu May 22, 2014 11:27 am

Re: best code organization to read multiple sensors

Post by lesto »

i see.
i like a lot the idea to pass the sensor itself on the reading, and meyba drop there a void* so the user can add it0s own structure needed for arcane extrapolation.

"For example I do not make the driver initialize the EXTConfig, I leave this to the user.
Instead there is a generic "ext"-callback that shall be used for sensor interrupts."
don't get this, even looking at code. i would like to hide EXTConfig to user, just tell me with pin the interrupt use :)

"Also I added so, if it is important, that the rounding errors in the polled time does not accumul'ate over time."
if sensor is in free-running mode. rounding error is 1 clock cicle, so loosing a read every minute is not a great problem.
if it is in sigle read, the error is less bigger as the next reading start after the read.

i like the idea of having a queue of more readings, and it is nice that priority sensor are put to the head, but they all end up to ONE dispachte thread, and this is bad for parallelization; only the final data readed should be sincronized to one thread.
Also like a lot the idea of injecting sensor read; but imho should use something like the interrupt handler (or iterrupt handler and poller handler call the inject, with is more a dispachet to the right thread's queue)

also virtual timer should be translated to only one tread with poll the next reading, as i "fell" it lighter than multiple virtual timer.

Queue can be a circular array instead of a list, and you just move the ntex of head and tail, so even here you have a static initialization and if that index are just byte (volatile) i think you don't even need READ lock, just a way to get the next (or head) index to write.

i've to think a lot, my head is sping around many ideas and i've to find plausible, and maybe also do some test (well think about how do some test) to measure the ovehead of the system.
User avatar
Korken
Posts: 270
Joined: Wed Apr 02, 2014 4:09 pm
Has thanked: 5 times
Been thanked: 6 times

Re: best code organization to read multiple sensors

Post by Korken »

Hmm, I have a few comments. :)

"For example I do not make the driver initialize the EXTConfig, I leave this to the user.
Instead there is a generic "ext"-callback that shall be used for sensor interrupts."
don't get this, even looking at code. i would like to hide EXTConfig to user, just tell me with pin the interrupt use :)
This is because it makes things happen "behind the scenes".
If someone deigns an embedded system, this person will know how the interrupts and such are connected to the system and with that one structure should define this.
Plus if you do it the way I do the EXTConfig will be a constant. No erroneous code can make strange things happen to the interrupts, which would be a pain to debug.

On top of this, if you want to mix sensor interrupts with other ones a single constant EXTConfig is the best I believe.
But this is a personal taste. I very strongly believe that as much as possible shall be constants, especially things that are constant after compile time (ex the sensors will not change their interrupt channel so it shall be a constant).


"Also I added so, if it is important, that the rounding errors in the polled time does not accumul'ate over time."
if sensor is in free-running mode. rounding error is 1 clock cicle, so loosing a read every minute is not a great problem.
if it is in sigle read, the error is less bigger as the next reading start after the read
Lets not be hasty here :)
If you do measurements over a very long time you do not want you measurements to drift over time, this would make the analysis of the data more difficult.
But I agree, for most applications this will not be used.


i like the idea of having a queue of more readings, and it is nice that priority sensor are put to the head, but they all end up to ONE dispachte thread, and this is bad for parallelization; only the final data readed should be sincronized to one thread.
Also like a lot the idea of injecting sensor read; but imho should use something like the interrupt handler (or iterrupt handler and poller handler call the inject, with is more a dispachet to the right thread's queue)
On the "one thread"-part, of course it will be one thread - why should it not be one thread? The only reason you'd might need more threads is if the sensors are on different buses, but then we get into a whole new level of problem.
To be "optimal" you should have one thread per bus, so no one bus is waiting for another bus. In my implementation I assumed that the sensors are on the same bus or that the read_sensor(void *) function executes very quickly.
For example the read_sensor function could be used to just start an asynchronous read of a sensor and the callback handles the data transitions.

Or if you want the thread to handle the entire transmit/receive operation you can add some more logic for multiple buses. This is how I would do it then:
1. Try to take the mutex for the bus the sensor that has requested a read occupies.
2. If it is already taken, move this read to a "waiting" queue.
3. Try the next sensor. .....
And in between each sensor for the original queue you test the mutex in the waiting queue to see if it has been released.
Fron the top of my head it feels as if it should minimize the overhead. But then you must know which bus each sensor uses.

I'm not quite sure about what you mean with the inject part. I designed it to be a user's way to manually read a sensor.
Of course it can be expanded to do much more if needed.

also virtual timer should be translated to only one tread with poll the next reading, as i "fell" it lighter than multiple virtual timer.
Each to his taste :)
I have found the virtual timer implementation to be extremely efficient. (What is the theoretical limit to this Giovanni?) I have successfully run 30 VTs with a 10kHz SysTick, when I was testing them for the first time, without any noticeable delays.
This was however on a Cortex-M4 CPU at 168 MHz, so perhaps not that strange.


Queue can be a circular array instead of a list, and you just move the ntex of head and tail, so even here you have a static initialization and if that index are just byte (volatile) i think you don't even need READ lock, just a way to get the next (or head) index to write.
Of course you can create a circular buffer, but I really like the queue system. Again, each to his taste :)
Both because it has inherit synchronization primitives and it is easy to use.


i've to think a lot, my head is sping around many ideas and i've to find plausible, and maybe also do some test (well think about how do some test) to measure the ovehead of the system.

Yeah, this is no easy task to do. If you have more ideas please put them here and we can discuss. With some luck we can converge to a single way of doing this and in the process get a very good and efficient way of working with sensors in a generic way. :)
And when it comes to measuring the overhead, you can do that with the TM module in 3.0.0. I'm not quite sure what it is in 2.6.x as I almost directly started with 3.0.0.

Good luck, keep it up and keep me updated! :D
lesto
Posts: 55
Joined: Thu May 22, 2014 11:27 am

Re: best code organization to read multiple sensors

Post by lesto »

sorry for late answer, but i'm taket at work and i'm not in the mood to code at home, so i will not answer about ext and reading drift, as i've to look again to your code.

On the "one thread"-part, of course it will be one thread - why should it not be one thread? The only reason you'd might need more threads is if the sensors are on different buses, but then we get into a whole new level of problem.

exactly. the first question was also tellong abut how create a "bus and dependency matrix" to undestand how many thread spawn and what they should do. For example, on stm32f3discovery you get gyro on SPI and acc+magne on I2C. Two thread? nope! they share the DMA, so same thread. But GPS is on serial, so different thread. And USB is on different bus, so another thread!
note that outcoming message may be hacked inside a sensors with interrupt handler is called by software (or message are injected in the queue, as you do), in the read_sensor() they will "simply" do other things; abstraction after abstraction this is becoming a scheduler :D


my idea is a bit different from yours:
1. interrupt/poll is triggered
2. they all call a "sorter function" passing some data on them (sensor triggerer and its bus) (note that is just a funcion, actually no other contex/thread/lock has happened, so the funtion must not use global values, unless taking care of this)
3. the function, based on the bus take the right thread queue and insert the read request (your queuse is in the language but are dinamic as i can tell. bad, if they are i would use a "read bit" for every sensor, witch is cleared after the read; if a sensor is already into queue you should not add another reading, IMHO, you have a bigger problem of bad cpu usage, so a read bit is fine because it can't be set two times, and if you find it already set you can send a debig message like "sensor reading is too slow for polling/interrupt speed"). function end.
4. because the specific thread queue/"listened bit"(to find here the lighwest way to wake a "waitng" thread, a signal may be good) has changed, the right thread wake and do the reading. If using bit, the order in with sensor are in the bit field is the way hey are readed (giving a priority), and also priority may be given to thread (so SPI/I2C to sensor may be high priority, SPI to SD, USB and serial may use lowest priority as they should never compromize "the algoritm" cpu time, but run in the spare cicle.


ps. about the injection i mean that a user may add a sensor read request to queue in your case, in my case he just need to call the "sorter function", but the concept is really similar, as mine is just an abstraction witch dispatch to multiple queue/read bit.

(more and more i think abut sensor's read bit and more i like the idea. just flip my bit -and send the right wake signal, but is everything handled by the "sorter function", even the "bit alredy flip error"- and let the right tread take care of your reading. obviusly special thing as writing to serial/usb will need a buffer/queue to queue message, so we may have to thing about this a bit more)

[queue]I have found the virtual timer implementation to be extremely efficient.[/queue]
maybe it does so in the background, but i don't think they will give you systick preciosion (or more, if you managed to handle that!), also i'm not sure "alarm" may be added statically. but i'm still such a noob with chibos, that i may say a lot of bad things.
I came from 8-bit world, so maybe i'm a bit too much concerned about saving cpu and ram, but please note that thos concern are about code architecture, and not about implementation. having a timer, or a thread, or an interrupt, or the algoritm, or an alien calling the "sorter function" does not make any difference, so we can just use the timer (as already coded) solution and then add whater we may fell "better" later and even check the result (just change the "thread reader" to print the exact sysclock in witch has been called, and you will measure the overall overhead, even between multiple thread. this trick is usefull to measure a lot of things about sensor-to-specilized-thread time)
Post Reply