-
Notifications
You must be signed in to change notification settings - Fork 0
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
Controller Module Header File #34
Open
riyapatel25
wants to merge
7
commits into
main
Choose a base branch
from
controller
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+234
−2
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a7b115d
Controller Module
riyapatel25 c043a30
formatting fix
riyapatel25 507cd6a
updated header file and controller initialization
SAT-oO 8e8f84b
controller initialization
SAT-oO 915f408
small fixes
SAT-oO 5e4884e
use of snack case for functions
SAT-oO cb390d6
included canard coefficient CL and angle delta, plus math.h included
SAT-oO File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
#include "controller.h" | ||
|
||
|
||
QueueHandle_t internalStateQueue; | ||
/** | ||
* Initialize controller module | ||
* Must be called before RTOS scheduler starts | ||
* @return true if initialization successful | ||
*/ | ||
bool controller_init(void) { | ||
|
||
// 1. Create internal state queue | ||
internalStateQueue = xQueueCreate(2, sizeof(float)); | ||
if (internalStateQueue == NULL) { | ||
logInfo("controller", "Error: Internal state queue creation failed.\n"); | ||
return false; | ||
} | ||
|
||
// 2. Initialize structs | ||
|
||
// 2.1 Gain table and flight conditions would go here (not shown) | ||
|
||
// 2.2 Controller initialization with memory checks | ||
float* g0 = (float*)malloc(sizeof(float)); | ||
if (g0 == NULL) { | ||
logInfo("controller", "Error: Memory allocation for g0 failed.\n"); | ||
return false; | ||
} | ||
*g0 = 9.8f; // Zero height gravity | ||
|
||
float* air_R = (float*)malloc(sizeof(float)); | ||
if (air_R == NULL) { | ||
logInfo("controller", "Error: Memory allocation for air_R failed.\n"); | ||
|
||
return false; | ||
} | ||
*air_R = 287.0579f; // Specific gas constant for air | ||
|
||
float* T_B = (float*)malloc(sizeof(float)); | ||
if (T_B == NULL) { | ||
logInfo("controller", "Error: Memory allocation for T_B failed.\n"); | ||
|
||
return false; | ||
} | ||
*T_B = 288.15f; // Troposphere base temperature | ||
|
||
float* P_B = (float*)malloc(sizeof(float)); | ||
if (P_B == NULL) { | ||
logInfo("controller", "Error: Memory allocation for P_B failed.\n"); | ||
|
||
return false; | ||
} | ||
*P_B = 101325.0f; // Troposphere base pressure | ||
|
||
float* P = (float*)malloc(sizeof(float)); | ||
if (P == NULL) { | ||
logInfo("controller", "Error: Memory allocation for P failed.\n"); | ||
|
||
return false; | ||
} | ||
*P = 101300.0f; // Initial pressure | ||
|
||
// Compute quaternion attitude | ||
float* phi = (float*)malloc(sizeof(float)); // Initial pitch angle -- deg2rad(-5) | ||
if (phi == NULL) { | ||
logInfo("controller", "Error: Memory allocation for phi failed.\n"); | ||
|
||
return false; | ||
} | ||
*phi = (-5.0f) * M_PI / 180.0f; // Convert -5 degrees to radians | ||
|
||
float* d = (float*)malloc(3 * sizeof(float)); // Axis of rotation | ||
if (d == NULL) { | ||
logInfo("controller", "Error: Memory allocation for d failed.\n"); | ||
|
||
return false; | ||
} | ||
d[0] = 0.0f; | ||
d[1] = 1.0f; | ||
d[2] = 0.0f; | ||
|
||
// Initialize controller_t structure | ||
controller_t controller_var = { | ||
.current_state = { | ||
.attitude = { | ||
.w = cos(*phi / 2), | ||
.x = d[0] * sin(*phi / 2), | ||
.y = d[1] * sin(*phi / 2), | ||
.z = d[2] * sin(*phi / 2) | ||
}, | ||
.rates = { .x = 0.0f, .y = 0.0f, .z = 0.0f }, | ||
.velocity = { .x = 0.0f, .y = 0.0f, .z = 0.0f }, | ||
.altitude = -log((*P) / (*P_B)) * (*air_R) * (*T_B) / (*g0), | ||
.timestamp = 0 | ||
}, | ||
.last_error = FLIGHT_PHASE_NONE, | ||
.controller_active = false, | ||
.last_ms = 0, | ||
.can_send_errors = 0, | ||
.data_miss_counter = 0 | ||
}; | ||
|
||
// Free up memory after controller initialization | ||
free(g0); | ||
free(air_R); | ||
free(T_B); | ||
free(P_B); | ||
free(P); | ||
free(phi); | ||
free(d); | ||
|
||
// 2.4 controller_output_SE_t | ||
control_output_SE_t state_est_output = { | ||
.command_angle = 0.0f, | ||
.timestamp = 0 | ||
}; | ||
|
||
// 3. Log initialization status | ||
logInfo("controller", "Controller initialized.\n"); | ||
return true; | ||
} | ||
|
||
|
||
|
||
/** | ||
* Update controller with new state data - called by state estimation module | ||
* @param new_state Latest state estimate from state estimation | ||
* @return false if validation/queueing fails | ||
*/ | ||
bool controller_update_inputs(controller_state_t *new_state){ | ||
|
||
} | ||
|
||
/** | ||
* Get most recent control output - called by state estimation module | ||
* @param output Pointer to store output -> type defined in state_estimation.h | ||
* @return false if no output available | ||
*/ | ||
bool controller_get_latest_output(control_output_SE_t *output){ | ||
|
||
} | ||
|
||
/** | ||
* Controller task function for RTOS | ||
*/ | ||
void controller_task(void *argument){ | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#ifndef CONTROLLER_H_ | ||
#define CONTROLLER_H_ | ||
celery6 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#include "FreeRTOS.h" | ||
#include "state_estimation.h" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be "application/estimator/estimator.h" |
||
#include <stdbool.h> | ||
#include "flight_phase.h" | ||
#include "queue.h" | ||
|
||
#include "math.h" | ||
|
||
|
||
|
||
/* Enums/Types */ | ||
|
||
|
||
// input from state estimation module | ||
typedef struct { | ||
quaternion_t attitude; // Current attitude vector | ||
vector3d_t rates; // Current angular rates | ||
vector3d_t velocity; // Current velocity vector | ||
float altitude; // Current altitude | ||
float timestamp; // Timestamp in ms | ||
float canard_coeff_CL; // Canard coefficient | ||
float canard_angle_delta; // Canard angle | ||
} controller_state_t; | ||
|
||
|
||
// main controller state using in task | ||
typedef struct { | ||
controller_state_t current_state; | ||
flight_phase_t last_error; | ||
bool controller_active; | ||
uint32_t last_ms; | ||
uint32_t can_send_errors; | ||
uint32_t data_miss_counter; | ||
} controller_t; | ||
|
||
// flight_conditions_t | ||
|
||
|
||
/* defined in state est. | ||
typedef struct{ | ||
float command_angle; | ||
uint32_t timestamp; | ||
} control_output_SE_t; */ | ||
|
||
|
||
|
||
extern QueueHandle_t internalStateQueue; | ||
|
||
|
||
/** | ||
* Initialize controller module | ||
* Must be called before RTOS scheduler starts | ||
* @return true if initialization successful | ||
*/ | ||
bool controller_init(void); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All the bools here should be replaced with w_status_t and include "rocketlib/include/common.h" |
||
|
||
/** | ||
* Update controller with new state data - called by state estimation module | ||
* @param new_state Latest state estimate from state estimation | ||
* @return false if validation/queueing fails | ||
*/ | ||
bool controller_update_inputs(controller_state_t *new_state); | ||
|
||
/** | ||
* Get most recent control output - called by state estimation module | ||
* @param output Pointer to store output -> type defined in state_estimation.h | ||
* @return false if no output available | ||
*/ | ||
bool controller_get_latest_output(control_output_SE_t *output); | ||
|
||
/** | ||
* Controller task function for RTOS | ||
*/ | ||
void controller_task(void *argument); | ||
|
||
#endif |
Submodule canlib
updated
25 files
+2 −2 | can.h | |
+0 −919 | can_common.c | |
+0 −459 | can_common.h | |
+10 −5 | canlib.h | |
+7 −8 | dspic33epxxxgp50x/dspic33epxxxgp50x_can.c | |
+18 −4 | mcp2515/mcp_2515.c | |
+130 −0 | message/msg_actuator.c | |
+60 −0 | message/msg_actuator.h | |
+96 −0 | message/msg_common.c | |
+40 −0 | message/msg_common.h | |
+196 −0 | message/msg_general.c | |
+108 −0 | message/msg_general.h | |
+246 −0 | message/msg_gps.c | |
+96 −0 | message/msg_gps.h | |
+93 −0 | message/msg_recovery.c | |
+44 −0 | message/msg_recovery.h | |
+229 −0 | message/msg_sensor.c | |
+93 −0 | message/msg_sensor.h | |
+80 −0 | message/msg_state_est.c | |
+35 −0 | message/msg_state_est.h | |
+95 −71 | message_types.h | |
+17 −5 | pic18f26k83/pic18f26k83_can.c | |
+0 −61 | pic18f26k83/pic18f26k83_timer.c | |
+0 −17 | pic18f26k83/pic18f26k83_timer.h | |
+1 −1 | stm32h7/stm32h7_can.c |
Submodule rocketlib
updated
18 files
+20 −0 | .github/workflows/clang-format-check.yml | |
+0 −20 | .github/workflows/clang-format-test.yml | |
+1 −0 | .gitignore | |
+13 −2 | README.md | |
+26 −2 | common/low_pass_filter.c | |
+3 −3 | doc/conf.py | |
+2 −2 | doc/i2c_driver.rst | |
+1 −0 | doc/index.rst | |
+109 −0 | doc/low_pass_filter.rst | |
+2 −2 | doc/millis.rst | |
+141 −41 | doc/pwm_driver.rst | |
+3 −0 | include/common.h | |
+20 −2 | include/low_pass_filter.h | |
+48 −12 | include/pwm.h | |
+66 −0 | pic18f26k83/pwm.c | |
+0 −78 | pic18f26k83/softpwm.c | |
+0 −4 | pic18f26k83/timer.c | |
+65 −5 | unit_tests/low_pass_filter_test.c |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are we dynamically allocating all theese variables?