Skip to content

Commit

Permalink
feat: some (bad) initial course example code
Browse files Browse the repository at this point in the history
  • Loading branch information
oystub committed Sep 17, 2024
1 parent 1dacb4c commit e976103
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/modules/px4_course/CMakeLists.txt
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
)
5 changes: 5 additions & 0 deletions src/modules/px4_course/Kconfig
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
14 changes: 14 additions & 0 deletions src/modules/px4_course/module.yaml
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
91 changes: 91 additions & 0 deletions src/modules/px4_course/px4_course.cpp
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);
}
53 changes: 53 additions & 0 deletions src/modules/px4_course/px4_course.h
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};
};

0 comments on commit e976103

Please sign in to comment.