Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for sensor polling at a specific frequency #38

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

allosaurus7000
Copy link

@allosaurus7000 allosaurus7000 commented Nov 22, 2020

Closes #29.

@matthewcn56
Copy link

Current pull request right now has the frequency as a constant once per second, but we're supposed to make it so that each sensor can get polled at different rates right?

@matthewcn56
Copy link

Would having a polling frequency being added into the constructor of Sensor make more sense so it can be variable for each sensor?

@allosaurus7000
Copy link
Author

allosaurus7000 commented Dec 7, 2020 via email

@allosaurus7000
Copy link
Author

allosaurus7000 commented Dec 7, 2020 via email

@harrisonCassar harrisonCassar changed the title Polling frequency updates Add support for sensor polling at a specific frequency Jan 11, 2021
Copy link
Contributor

@harrisonCassar harrisonCassar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A good start! See below:

@@ -29,6 +31,7 @@ namespace spartan {
// Data getters
int getBusID() const;
int getInstance() const;
const long frequency = 1000;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a styling/lint issue: try to keep the indentation the same if possible!

Comment on lines +2 to 3
#include "utilis.h"
// Constructor
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a styling/lint issue: adding a newline between the include directives and the rest of the source code helps keep the file more readable.

Comment on lines +23 to +24
virtual long last_polled() = 0;
virtual unsigned long poll_correct_frequency();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a styling/lint issue: try to keep the indentation the same if possible!

Comment on lines +7 to +11
unsigned long spartan::Sensor::poll_correct_frequency() {
if (getTimeMillis() > last_polled + frequency) {
last_polled = getTimeMillis();
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on what is shown here, I presume this method is meant to provide the Sensor child class with the ability to check if it should poll during this invocation by checking if enough time has passed since the last time it polled the sensor.

This is good! This is needed in some fashion in order to implement the functionality we desire: polling at different frequencies.

Currently, however, there's a few problems:

  • I don't seem to see the last_polled variable defined anywhere at all in the Sensor abstract based class... this should be resulting in a compilation/build error right now. Perhaps add a private member variable that holds that information? Maybe named m_last_poll or something along those lines?
  • The current name for this method poll_correct_frequency I don't think properly reflects the intent of the function in my eyes. If the method's intent is as what I outlined above, then I would think something like is_poll_ready... or something along those lines (I actually don't like my name here, so maybe something else 😄 ).
  • The use of frequency might be a bit ambiguous, as we are using it as a "period" of time instead of a "number of polls per unit of time". Perhaps change this name to something like polling_period?
  • This method doesn't actually return any value (needs to return of type unsigned long). This should be causing a compilation error.

@@ -29,6 +31,7 @@ namespace spartan {
// Data getters
int getBusID() const;
int getInstance() const;
const long frequency = 1000;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifying this variable that represents the frequency/period of polling should likely not be something we want to be in the public access section of our base Sensor class. By doing so, we allow any other portion of the FSW outside of the class to freely modify this frequency for any of the instantiated Sensor child objects. This could be error-prone, as we expect only the Sensor class and (potentially) its derived classes to modify this frequency variable.

In other words, this is something that should only be handled by the internal methods of the Sensor base class, since we want to ensure that we're manipulating this frequency in an expected and safe manner. At this point, we expect this frequency to be defined by compile-time, and it should be used when instantiating a new object for a Sensor-derived class. If we want to support run-time frequency changes, we should only allow access of this private member variable through a protected "setter" method that can be exclusively accessed by Sensor and its derived classes.

Here's my quick thoughts:

  • Move this frequency variable from public to private.
  • Add some accessor functions (you might not even need these depending on how you implement/what we want), including a "getter" and a "setter". Let's just start with the "getter" as public, and the "setter" as a protected method (this will support potential dynamic frequencies in the future!).
  • Rename this frequency variable to more accurately represent the data it represents (refer to previous comment I made on this review).

@@ -29,6 +31,7 @@ namespace spartan {
// Data getters
int getBusID() const;
int getInstance() const;
const long frequency = 1000;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a particular reason why we have our "default" value as 1000 milliseconds?

When making some of the other changes I mentioned in this review, remember that we want our Sensor-derived classes to themselves define their own frequencies... using an initializer list for this would probably be the best option.

@harrisonCassar harrisonCassar self-requested a review January 11, 2021 22:52
Copy link
Contributor

@harrisonCassar harrisonCassar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the current changes seem to mostly aim to address the first part of what Issue #29's description mentions. Don't forget to take a look at the other portions too!

In summary, this is to properly update the main flight loop in main.cpp to actually poll the sensors at these desired frequencies, as well as updating the Sensor-derived classes to obey the new functionality/interface.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add sensor polling frequency/period
3 participants