Convert aos over to flatbuffers
Everything builds, and all the tests pass. I suspect that some entries
are missing from the config files, but those will be found pretty
quickly on startup.
There is no logging or live introspection of queue messages.
Change-Id: I496ee01ed68f202c7851bed7e8786cee30df29f5
diff --git a/frc971/codelab/BUILD b/frc971/codelab/BUILD
index 0fe9205..6302443 100644
--- a/frc971/codelab/BUILD
+++ b/frc971/codelab/BUILD
@@ -1,6 +1,6 @@
package(default_visibility = ["//visibility:public"])
-load("//aos/build:queues.bzl", "queue_library")
+load("@com_github_google_flatbuffers//:build_defs.bzl", "flatbuffer_cc_library")
cc_binary(
name = "basic_test",
@@ -8,10 +8,9 @@
srcs = ["basic_test.cc"],
deps = [
":basic",
- ":basic_queue",
- "//aos:queues",
+ ":basic_fbs",
"//aos/controls:control_loop_test",
- "//aos/events:shm-event-loop",
+ "//aos/events:shm_event_loop",
"//aos/testing:googletest",
"//frc971/control_loops:state_feedback_loop",
"//frc971/control_loops:team_number_test_environment",
@@ -23,18 +22,14 @@
srcs = ["basic.cc"],
hdrs = ["basic.h"],
deps = [
- ":basic_queue",
+ ":basic_fbs",
"//aos/controls:control_loop",
],
)
-queue_library(
- name = "basic_queue",
+flatbuffer_cc_library(
+ name = "basic_fbs",
srcs = [
- "basic.q",
- ],
- deps = [
- "//aos/controls:control_loop_queues",
- "//frc971/control_loops:queues",
+ "basic.fbs",
],
)
diff --git a/frc971/codelab/basic.cc b/frc971/codelab/basic.cc
index d06e285..29ffcf5 100644
--- a/frc971/codelab/basic.cc
+++ b/frc971/codelab/basic.cc
@@ -4,12 +4,12 @@
namespace codelab {
Basic::Basic(::aos::EventLoop *event_loop, const ::std::string &name)
- : aos::controls::ControlLoop<BasicQueue>(event_loop, name) {}
+ : aos::controls::ControlLoop<Goal, Position, Status, Output>(event_loop,
+ name) {}
-void Basic::RunIteration(const BasicQueue::Goal *goal,
- const BasicQueue::Position *position,
- BasicQueue::Output *output,
- BasicQueue::Status *status) {
+void Basic::RunIteration(const Goal *goal, const Position *position,
+ aos::Sender<Output>::Builder *output,
+ aos::Sender<Status>::Builder *status) {
// TODO(you): Set the intake_voltage to 12 Volts when
// intake is requested (via intake in goal). Make sure not to set
// the motor to anything but 0 V when the limit_sensor is pressed.
diff --git a/frc971/codelab/basic.fbs b/frc971/codelab/basic.fbs
new file mode 100644
index 0000000..82c9607
--- /dev/null
+++ b/frc971/codelab/basic.fbs
@@ -0,0 +1,35 @@
+namespace frc971.codelab;
+
+// The theme of this basic test is a simple intake system.
+//
+// The system will have a motor driven by the voltage returned
+// by output, and then eventually this motor, when run enough,
+// will trigger the limit_sensor. The hypothetical motor should shut
+// off in that hypothetical situation to avoid hypothetical burnout.
+table Goal {
+ // The control loop needs to intake now.
+ intake:bool;
+}
+
+table Position {
+ // This is a potential incoming sensor value letting us know
+ // if we need to be intaking.
+ limit_sensor:bool;
+}
+
+table Status {
+ // Lets consumers of basic_queue.status know if
+ // the requested intake is finished.
+ intake_complete:bool;
+}
+
+table Output {
+ // This would be set up to drive a hypothetical motor that would
+ // hope to intake something.
+ intake_voltage:double;
+}
+
+root_type Goal;
+root_type Position;
+root_type Status;
+root_type Output;
diff --git a/frc971/codelab/basic.h b/frc971/codelab/basic.h
index 6f33718..acaec0c 100644
--- a/frc971/codelab/basic.h
+++ b/frc971/codelab/basic.h
@@ -4,7 +4,7 @@
#include "aos/controls/control_loop.h"
#include "aos/time/time.h"
-#include "frc971/codelab/basic.q.h"
+#include "frc971/codelab/basic_generated.h"
namespace frc971 {
namespace codelab {
@@ -41,16 +41,16 @@
// Order of approaching this should be:
// - Read the BUILD file and learn about what code is being generated.
// - Read basic.q, and familiarize yourself on the inputs and types involved.
-class Basic : public ::aos::controls::ControlLoop<BasicQueue> {
+class Basic
+ : public ::aos::controls::ControlLoop<Goal, Position, Status, Output> {
public:
explicit Basic(::aos::EventLoop *event_loop,
- const ::std::string &name = ".frc971.codelab.basic_queue");
+ const ::std::string &name = "/codelab");
protected:
- void RunIteration(const BasicQueue::Goal *goal,
- const BasicQueue::Position *position,
- BasicQueue::Output *output,
- BasicQueue::Status *status) override;
+ void RunIteration(const Goal *goal, const Position *position,
+ aos::Sender<Output>::Builder *output,
+ aos::Sender<Status>::Builder *status) override;
};
} // namespace codelab
diff --git a/frc971/codelab/basic.q b/frc971/codelab/basic.q
deleted file mode 100644
index 58fd69e..0000000
--- a/frc971/codelab/basic.q
+++ /dev/null
@@ -1,44 +0,0 @@
-package frc971.codelab;
-
-import "aos/controls/control_loops.q";
-import "frc971/control_loops/control_loops.q";
-
-// The theme of this basic test is a simple intake system.
-//
-// The system will have a motor driven by the voltage returned
-// by output, and then eventually this motor, when run enough,
-// will trigger the limit_sensor. The hypothetical motor should shut
-// off in that hypothetical situation to avoid hypothetical burnout.
-queue_group BasicQueue {
- implements aos.control_loops.ControlLoop;
-
- message Goal {
- // The control loop needs to intake now.
- bool intake;
- };
-
- message Position {
- // This is a potential incoming sensor value letting us know
- // if we need to be intaking.
- bool limit_sensor;
- };
-
- message Status {
- // Lets consumers of basic_queue.status know if
- // the requested intake is finished.
- bool intake_complete;
- };
-
- message Output {
- // This would be set up to drive a hypothetical motor that would
- // hope to intake something.
- double intake_voltage;
- };
-
- queue Goal goal;
- queue Position position;
- queue Output output;
- queue Status status;
-};
-
-queue_group BasicQueue basic_queue;
diff --git a/frc971/codelab/basic_test.cc b/frc971/codelab/basic_test.cc
index 91059f6..487a09b 100644
--- a/frc971/codelab/basic_test.cc
+++ b/frc971/codelab/basic_test.cc
@@ -6,9 +6,8 @@
#include <memory>
#include "aos/controls/control_loop_test.h"
-#include "aos/events/shm-event-loop.h"
-#include "aos/queue.h"
-#include "frc971/codelab/basic.q.h"
+#include "aos/events/shm_event_loop.h"
+#include "frc971/codelab/basic_generated.h"
#include "frc971/control_loops/team_number_test_environment.h"
#include "gtest/gtest.h"
@@ -25,12 +24,9 @@
public:
BasicSimulation(::aos::EventLoop *event_loop, chrono::nanoseconds dt)
: event_loop_(event_loop),
- position_sender_(event_loop_->MakeSender<BasicQueue::Position>(
- ".frc971.codelab.basic_queue.position")),
- status_fetcher_(event_loop_->MakeFetcher<BasicQueue::Status>(
- ".frc971.codelab.basic_queue.status")),
- output_fetcher_(event_loop_->MakeFetcher<BasicQueue::Output>(
- ".frc971.codelab.basic_queue.output")) {
+ position_sender_(event_loop_->MakeSender<Position>("/codelab")),
+ status_fetcher_(event_loop_->MakeFetcher<Status>("/codelab")),
+ output_fetcher_(event_loop_->MakeFetcher<Output>("/codelab")) {
event_loop_->AddPhasedLoop(
[this](int) {
// Skip this the first time.
@@ -45,11 +41,13 @@
// Sends a queue message with the position data.
void SendPositionMessage() {
- auto position = position_sender_.MakeMessage();
+ auto builder = position_sender_.MakeBuilder();
- position->limit_sensor = limit_sensor_;
+ Position::Builder position_builder = builder.MakeBuilder<Position>();
- position.Send();
+ position_builder.add_limit_sensor(limit_sensor_);
+
+ builder.Send(position_builder.Finish());
}
void VerifyResults(double voltage, bool status) {
@@ -59,8 +57,8 @@
ASSERT_TRUE(output_fetcher_.get() != nullptr);
ASSERT_TRUE(status_fetcher_.get() != nullptr);
- EXPECT_EQ(output_fetcher_->intake_voltage, voltage);
- EXPECT_EQ(status_fetcher_->intake_complete, status);
+ EXPECT_EQ(output_fetcher_->intake_voltage(), voltage);
+ EXPECT_EQ(status_fetcher_->intake_complete(), status);
}
void set_limit_sensor(bool value) { limit_sensor_ = value; }
@@ -71,9 +69,9 @@
private:
::aos::EventLoop *event_loop_;
- ::aos::Sender<BasicQueue::Position> position_sender_;
- ::aos::Fetcher<BasicQueue::Status> status_fetcher_;
- ::aos::Fetcher<BasicQueue::Output> output_fetcher_;
+ ::aos::Sender<Position> position_sender_;
+ ::aos::Fetcher<Status> status_fetcher_;
+ ::aos::Fetcher<Output> output_fetcher_;
bool limit_sensor_ = false;
@@ -83,13 +81,41 @@
class BasicControlLoopTest : public ::aos::testing::ControlLoopTest {
public:
BasicControlLoopTest()
- : ::aos::testing::ControlLoopTest(chrono::microseconds(5050)),
+ : ::aos::testing::ControlLoopTest(
+ "{\n"
+ " \"channels\": [ \n"
+ " {\n"
+ " \"name\": \"/aos\",\n"
+ " \"type\": \"aos.JoystickState\"\n"
+ " },\n"
+ " {\n"
+ " \"name\": \"/aos\",\n"
+ " \"type\": \"aos.RobotState\"\n"
+ " },\n"
+ " {\n"
+ " \"name\": \"/codelab\",\n"
+ " \"type\": \"frc971.codelab.Goal\"\n"
+ " },\n"
+ " {\n"
+ " \"name\": \"/codelab\",\n"
+ " \"type\": \"frc971.codelab.Output\"\n"
+ " },\n"
+ " {\n"
+ " \"name\": \"/codelab\",\n"
+ " \"type\": \"frc971.codelab.Status\"\n"
+ " },\n"
+ " {\n"
+ " \"name\": \"/codelab\",\n"
+ " \"type\": \"frc971.codelab.Position\"\n"
+ " }\n"
+ " ]\n"
+ "}\n",
+ chrono::microseconds(5050)),
test_event_loop_(MakeEventLoop()),
- goal_sender_(test_event_loop_->MakeSender<BasicQueue::Goal>(
- ".frc971.codelab.basic_queue.goal")),
+ goal_sender_(test_event_loop_->MakeSender<Goal>("/codelab")),
basic_event_loop_(MakeEventLoop()),
- basic_(basic_event_loop_.get(), ".frc971.codelab.basic_queue"),
+ basic_(basic_event_loop_.get(), "/codelab"),
basic_simulation_event_loop_(MakeEventLoop()),
basic_simulation_(basic_simulation_event_loop_.get(), dt()) {
@@ -97,7 +123,7 @@
}
::std::unique_ptr<::aos::EventLoop> test_event_loop_;
- ::aos::Sender<BasicQueue::Goal> goal_sender_;
+ ::aos::Sender<Goal> goal_sender_;
::std::unique_ptr<::aos::EventLoop> basic_event_loop_;
Basic basic_;
@@ -109,9 +135,10 @@
// Tests that when the motor has finished intaking it stops.
TEST_F(BasicControlLoopTest, IntakeLimitTransitionsToTrue) {
{
- auto message = goal_sender_.MakeMessage();
- message->intake = true;
- ASSERT_TRUE(message.Send());
+ auto builder = goal_sender_.MakeBuilder();
+ Goal::Builder goal_builder = builder.MakeBuilder<Goal>();
+ goal_builder.add_intake(true);
+ ASSERT_TRUE(builder.Send(goal_builder.Finish()));
}
basic_simulation_.set_limit_sensor(false);
@@ -121,9 +148,10 @@
basic_simulation_.VerifyResults(12.0, false);
{
- auto message = goal_sender_.MakeMessage();
- message->intake = true;
- ASSERT_TRUE(message.Send());
+ auto builder = goal_sender_.MakeBuilder();
+ Goal::Builder goal_builder = builder.MakeBuilder<Goal>();
+ goal_builder.add_intake(true);
+ ASSERT_TRUE(builder.Send(goal_builder.Finish()));
}
basic_simulation_.set_limit_sensor(true);
@@ -136,9 +164,10 @@
// and intake is requested.
TEST_F(BasicControlLoopTest, IntakeLimitNotSet) {
{
- auto message = goal_sender_.MakeMessage();
- message->intake = true;
- ASSERT_TRUE(message.Send());
+ auto builder = goal_sender_.MakeBuilder();
+ Goal::Builder goal_builder = builder.MakeBuilder<Goal>();
+ goal_builder.add_intake(true);
+ ASSERT_TRUE(builder.Send(goal_builder.Finish()));
}
basic_simulation_.set_limit_sensor(false);
@@ -151,9 +180,10 @@
// even if the limit sensor is off.
TEST_F(BasicControlLoopTest, NoIntakeLimitNotSet) {
{
- auto message = goal_sender_.MakeMessage();
- message->intake = false;
- ASSERT_TRUE(message.Send());
+ auto builder = goal_sender_.MakeBuilder();
+ Goal::Builder goal_builder = builder.MakeBuilder<Goal>();
+ goal_builder.add_intake(false);
+ ASSERT_TRUE(builder.Send(goal_builder.Finish()));
}
basic_simulation_.set_limit_sensor(false);
@@ -166,9 +196,10 @@
// is pressed and intake is requested.
TEST_F(BasicControlLoopTest, IntakeLimitSet) {
{
- auto message = goal_sender_.MakeMessage();
- message->intake = true;
- ASSERT_TRUE(message.Send());
+ auto builder = goal_sender_.MakeBuilder();
+ Goal::Builder goal_builder = builder.MakeBuilder<Goal>();
+ goal_builder.add_intake(true);
+ ASSERT_TRUE(builder.Send(goal_builder.Finish()));
}
basic_simulation_.set_limit_sensor(true);
@@ -180,9 +211,10 @@
// Tests that the intake is off if no intake is requested,
TEST_F(BasicControlLoopTest, NoIntakeLimitSet) {
{
- auto message = goal_sender_.MakeMessage();
- message->intake = false;
- ASSERT_TRUE(message.Send());
+ auto builder = goal_sender_.MakeBuilder();
+ Goal::Builder goal_builder = builder.MakeBuilder<Goal>();
+ goal_builder.add_intake(false);
+ ASSERT_TRUE(builder.Send(goal_builder.Finish()));
}
basic_simulation_.set_limit_sensor(true);
@@ -205,9 +237,10 @@
basic_simulation_.set_limit_sensor(true);
{
- auto message = goal_sender_.MakeMessage();
- message->intake = true;
- ASSERT_TRUE(message.Send());
+ auto builder = goal_sender_.MakeBuilder();
+ Goal::Builder goal_builder = builder.MakeBuilder<Goal>();
+ goal_builder.add_intake(true);
+ ASSERT_TRUE(builder.Send(goal_builder.Finish()));
}
SetEnabled(false);