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 control message expiration time #41

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/pod_communication.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#ifndef POD_COMMUNICATION_HPP
#define POD_COMMUNICATION_HPP

#include "pico/time.h"

struct LimControlMessage
{
float velocity;
float throttle;
absolute_time_t messageTime;
};

LimControlMessage read_control_message();
Expand Down
18 changes: 13 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
const float OPERATING_FREQUENCY = 45552.3;
const float OFFSET = 0.0411;

// Value will be changed by other core, so prevent compiler from optimizing as constant
volatile static LimControlMessage lcm{ 0, 1 };
const unsigned EXPIRATION_TIME = 28; // milliseconds

// Value may need to be volatile?
static LimControlMessage lcm{ 0, 1, get_absolute_time() };
// Allow only one core at a time to access lcm
static mutex lcmMutex;

Expand Down Expand Up @@ -71,8 +73,7 @@ void monitor_serial()
LimControlMessage message = read_control_message();

mutex_enter_blocking(&lcmMutex);
lcm.velocity = message.velocity;
lcm.throttle = message.throttle;
lcm = message;
mutex_exit(&lcmMutex);
}
}
Expand All @@ -83,7 +84,14 @@ void run_inverter()
while (true)
{
mutex_enter_blocking(&lcmMutex);
float frequency = calculate_frequency(lcm.velocity, lcm.throttle);

// Calculate time difference in microseconds and check if the difference is
// greater than the expiration time. If so, set frequency to 0. Otherwise,
// set frequency to the data received.
Comment on lines +88 to +90
Copy link
Member

Choose a reason for hiding this comment

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

Nitpick [non-blocking]: comment seems a bit too verbose, describing the code literally rather than explaining intent.

int64_t timeDiff = absolute_time_diff_us(lcm.messageTime, get_absolute_time());
bool hasExpired = timeDiff > EXPIRATION_TIME * 1000;
float frequency = hasExpired ? 0 : calculate_frequency(lcm.velocity, lcm.throttle);

mutex_exit(&lcmMutex);

// Set pins to low if frequency is zero
Expand Down
5 changes: 1 addition & 4 deletions src/pod_communication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,5 @@ LimControlMessage read_control_message()
float throttle;
std::cin >> velocity >> throttle;

std::cout << "Received velocity: " << velocity << std::endl;
std::cout << "Received throttle: " << throttle << std::endl;

return { velocity, throttle };
return { velocity, throttle, get_absolute_time() };
}