forked from PX4/PX4-Autopilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: some (bad) initial course example code
- Loading branch information
Showing
5 changed files
with
171 additions
and
0 deletions.
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,8 @@ | ||
px4_add_module ( | ||
MODULE modules__px4_course | ||
MAIN px4_course | ||
SRCS | ||
px4_course.cpp | ||
MODULE_CONFIG | ||
module.yaml | ||
) |
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,5 @@ | ||
menuconfig MODULES_PX4_COURSE | ||
bool "px4_course" | ||
default n | ||
---help--- | ||
Enable the Ascend PX4 course module |
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,14 @@ | ||
module_name: Ascend PX4 course | ||
|
||
parameters: | ||
- group: Example parameters | ||
definitions: | ||
PX4CS_NUM_GREET: | ||
description: | ||
short: How many times to greet | ||
long: | | ||
When running the course module, the program will say Hello" this many times :) | ||
type: int32 | ||
min: 1 | ||
max: 20 | ||
default: 2 |
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,91 @@ | ||
#include "px4_course.h" | ||
|
||
int PX4Course::print_usage(const char *reason){ | ||
printf("Use the PX4 course module however you like!\n"); | ||
return 0; | ||
} | ||
|
||
int PX4Course::task_spawn(int argc, char*argv[]){ | ||
_task_id = px4_task_spawn_cmd("module", | ||
SCHED_DEFAULT, | ||
SCHED_PRIORITY_DEFAULT, | ||
1024, | ||
(px4_main_t)&run_trampoline, | ||
(char *const *)argv); | ||
|
||
if (_task_id < 0) { | ||
_task_id = -1; | ||
return -errno; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
int PX4Course::custom_command(int argc, char *argv[]){ | ||
printf("User ran command %s with arguments \"", argv[0]); | ||
for (int i = 0; i < argc; ++i){ | ||
printf("%s ", argv[i]); | ||
} | ||
printf("\"\n"); | ||
return 1; | ||
} | ||
|
||
PX4Course *PX4Course::instantiate(int argc, char *argv[]) | ||
{ | ||
PX4Course *instance = new PX4Course(); | ||
if (instance == nullptr) { | ||
PX4_ERR("alloc failed"); | ||
} | ||
|
||
return instance; | ||
} | ||
|
||
PX4Course::PX4Course() | ||
: ModuleParams(nullptr) | ||
{ | ||
} | ||
|
||
void PX4Course::run() | ||
{ | ||
bool first_iteration{true}; | ||
while (!should_exit()) { | ||
|
||
sleep(20); | ||
PX4_INFO("PX4 course is%srunning!", first_iteration ? " " : " still "); | ||
|
||
// Add code to beep here! | ||
|
||
parameters_update(false); | ||
} | ||
|
||
} | ||
|
||
void PX4Course::parameters_update(bool force) | ||
{ | ||
// check for parameter updates | ||
if (_parameter_update_sub.updated() || force) { | ||
// clear update | ||
parameter_update_s update; | ||
_parameter_update_sub.copy(&update); | ||
|
||
// update parameters from storage | ||
updateParams(); | ||
} | ||
} | ||
|
||
int PX4Course::print_status(){ | ||
auto num_hellos = _param_px4cs_num_greet.get(); | ||
for (int i = 0; i < num_hellos; ++i){ | ||
printf("Hello! "); | ||
} | ||
printf("\n"); | ||
return 0; | ||
} | ||
|
||
/** | ||
* You don't need to change this function, it just calls main in the class. | ||
*/ | ||
extern "C" __EXPORT int px4_course_main(int argc, char *argv[]) | ||
{ | ||
return PX4Course::main(argc, argv); | ||
} |
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,53 @@ | ||
#pragma once | ||
|
||
#include <px4_platform_common/module.h> | ||
#include <px4_platform_common/module_params.h> | ||
#include <uORB/SubscriptionInterval.hpp> | ||
#include <uORB/topics/parameter_update.h> | ||
|
||
using namespace time_literals; | ||
|
||
extern "C" __EXPORT int px4_course_main(int argc, char *argv[]); | ||
|
||
// Note that the PX4Course inherits from the ModuleBase, which gives us some useful features | ||
|
||
class PX4Course : public ModuleBase<PX4Course>, public ModuleParams | ||
{ | ||
public: | ||
PX4Course(); | ||
|
||
virtual ~PX4Course() = default; | ||
|
||
/** @see ModuleBase */ | ||
static int task_spawn(int argc, char *argv[]); | ||
|
||
/** @see ModuleBase */ | ||
static PX4Course *instantiate(int argc, char *argv[]); | ||
|
||
/** @see ModuleBase */ | ||
static int custom_command(int argc, char *argv[]); | ||
|
||
/** @see ModuleBase */ | ||
static int print_usage(const char *reason = nullptr); | ||
|
||
/** @see ModuleBase::run() */ | ||
void run() override; | ||
|
||
/** @see ModuleBase::print_status() */ | ||
int print_status() override; | ||
|
||
private: | ||
/** | ||
* Check for parameter changes and update them if needed. | ||
* @param parameter_update_sub uorb subscription to parameter_update | ||
* @param force for a parameter update | ||
*/ | ||
void parameters_update(bool force = false); | ||
|
||
DEFINE_PARAMETERS( | ||
(ParamInt<px4::params::PX4CS_NUM_GREET>) _param_px4cs_num_greet | ||
) | ||
|
||
// Subscriptions | ||
uORB::SubscriptionInterval _parameter_update_sub{ORB_ID(parameter_update), 1_s}; | ||
}; |