Skip to content

Commit

Permalink
Made changes to address issue #14.
Browse files Browse the repository at this point in the history
* I now calculate output channels that are unused in the transitions and ensure that these are subscribed to and that they lead to off_script.
* I now use the fully qualified name for a channel including the 'in' and 'out' in the gen.xml file. In the internal data structures it isn't clear when this is a 'channel' or not, esp. intermediate representation.
* This now means that the b2x tests pass for the battery monitor.
  • Loading branch information
pefribeiro committed Jul 27, 2023
1 parent da38255 commit e135735
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 5 deletions.
6 changes: 3 additions & 3 deletions examples/bmon/gen.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<makefile />

<!-- Tell the generator that this channel maps to a float: -->
<channel name="batteryInfo" type="float" />
<!--<channel name="batteryInfo" type="float" />-->
</cpp>
<cpp variant="ros">
<catkin>
Expand All @@ -23,8 +23,8 @@
<include src="battery_monitor/InputAccepted.h" />

<!-- Tell the generator that this channel maps non-trivially to a ROS type: -->
<channel name="batteryInfo" type="sensor_msgs::BatteryState" topic="/fcs/battery_state" />
<channel name="batteryStatus" type="battery_monitor::BatteryStatus" topic="/battery_monitor/battery_status" />
<channel name="batteryInfo.in" type="sensor_msgs::BatteryState" topic="/fcs/battery_state" />
<channel name="batteryStatus.out" type="battery_monitor::BatteryStatus" topic="/battery_monitor/battery_status" />
<channel name="inputAccepted" type="battery_monitor::InputAccepted" topic="/battery_monitor/input_accepted" />

</cpp>
Expand Down
13 changes: 13 additions & 0 deletions internal/gen/config/cpp/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,31 @@ package cpp
import (
"github.com/UoY-RoboStar/rtcg/internal/stm"
"github.com/UoY-RoboStar/rtcg/internal/testlang/rstype"
"github.com/UoY-RoboStar/rtcg/internal/testlang/channel"
)

// Context contains any context derived from the C++ generator config.
//
// This is common to multiple different generators that consider C++ config.
type Context struct {
Includes []Include // Includes contains the user-configured includes.
Channels []channel.Channel // All channels
ChannelTypes map[string]ChannelType // ChannelTypes contains the calculated channel types.
HasConversion bool // HasConversion is true if there is a convert.cpp file.
ChannelTopics map[string]string // Mapping from channel name to topic.
// ChannelIO map[string] // Indicates whether a channel is input or output.
}

// Process processes a config into a Context.
// It expects the unified type-map from the testing suite.
func (c *Config) Process(types stm.TypeMap) Context {
ctx := Context{
Includes: c.Includes,
Channels: c.getChannels(),
ChannelTypes: make(map[string]ChannelType, len(types)),
ChannelTopics: c.ChannelTopicMap(),
HasConversion: false,
// ChannelIO: c.ChannelIO(),
}

overrides := c.ChannelMap()
Expand Down Expand Up @@ -50,3 +55,11 @@ type ChannelType struct {
func (t ChannelType) HasOverride() bool {
return t.Override != ""
}

// // ChannelIO defines whether a channel is input or output
// type ChannelIO uint8

// const (
// input ChannelIO = iota
// utput ChannelIO = iota
// )
39 changes: 37 additions & 2 deletions internal/gen/config/cpp/cpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package cpp
import (
"github.com/UoY-RoboStar/rtcg/internal/gen/config/catkin"
"github.com/UoY-RoboStar/rtcg/internal/gen/config/makefile"
"github.com/UoY-RoboStar/rtcg/internal/testlang/channel"
)

// Config contains configuration for a C++ generator.
Expand Down Expand Up @@ -41,12 +42,35 @@ func WithChannel(name, ty string) Option {
}
}

func (c *Config) getChannels() []channel.Channel {

//carray := make([]channel.Channel, len(c.Channels))
var carray []channel.Channel

for _, chi := range c.Channels {
ach := new(channel.Channel)
err := ach.UnmarshalText([]byte(chi.Name))
if (err == nil) {
carray = append(carray, *ach)
}
}

return carray
}

// ChannelMap gets the Channels field of this Config as a map from channel names to type overrides.
func (c *Config) ChannelMap() map[string]string {
cmap := make(map[string]string, len(c.Channels))

for _, over := range c.Channels {
cmap[over.Name] = over.Type

// Create object and parse
chp := new(channel.Channel)
err := chp.UnmarshalText([]byte(over.Name))

if err == nil {
cmap[chp.Name] = over.Type
}
}

return cmap
Expand All @@ -57,12 +81,23 @@ func (c *Config) ChannelTopicMap() map[string]string {
cmap := make(map[string]string, len(c.Channels))

for _, over := range c.Channels {
cmap[over.Name] = over.Topic

// Create object and parse
chp := new(channel.Channel)
err := chp.UnmarshalText([]byte(over.Name))

if err == nil {
cmap[chp.Name] = over.Topic
} else {
cmap[over.Name] = over.Topic
}
}

return cmap
}

// ChannelIO gets the Channel types


// Include captures a custom include header.
type Include struct {
Expand Down
11 changes: 11 additions & 0 deletions internal/gen/cpp/embed/templates/base/stm.cpp.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public:
{{- range .Transitions.Out }}
void {{ cppCallbackName .Channel.Name }}(const {{ cppChannelMsgType .Channel.Name }} msg);
{{- end }}
{{- range cppOtherOutChannels .Transitions.Out .Channels }}
void {{ cppCallbackName .Name }}(const {{ cppChannelMsgType .Name }} msg);
{{- end }}
private:
Verdict verdict_;
State state_ = {{ (index .Stm.States 0).ID | cppStateEnum }};
Expand Down Expand Up @@ -131,6 +134,14 @@ void StateMachine::{{ cppCallbackName .Channel.Name }}(const {{ cppChannelMsgTyp
}
{{- end }}

{{- range cppOtherOutChannels .Transitions.Out .Channels }}

void StateMachine::{{ cppCallbackName .Name }}(const {{ cppChannelMsgType .Name }} msg)
{
ROS_INFO_STREAM("Unexpected message for {{ . }}." << std::endl);
end(rtcg::Status::OFF_SCRIPT);
}
{{- end }}

//
// State machine entry functions
Expand Down
3 changes: 3 additions & 0 deletions internal/gen/cpp/embed/templates/ros/main.cpp.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ const int INPUT_DELAY_SEC = 3;
{{ range .Transitions.Out }}
auto {{ template "subscriber_name" . }} = nh.subscribe("{{ cppChannelTopicName $.ChannelTopics .Channel.Name }}", 10, &StateMachine::{{ cppCallbackName .Channel.Name }}, &stm);
{{- end }}
{{- range cppOtherOutChannels .Transitions.Out .Channels }}
auto {{ toLowerUnderscored .Name }}_sub = nh.subscribe("{{ cppChannelTopicName $.ChannelTopics .Name }}", 10, &StateMachine::{{ cppCallbackName .Name }}, &stm);
{{- end }}

// Timers
auto timeout_timer = nh.createTimer(ros::Duration(TIMEOUT_SEC), &StateMachine::timeoutCallback, &stm);
Expand Down
27 changes: 27 additions & 0 deletions internal/gen/cpp/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"strings"
"text/template"

"github.com/UoY-RoboStar/rtcg/internal/stm/transition"
"github.com/UoY-RoboStar/rtcg/internal/strmanip"
"github.com/UoY-RoboStar/rtcg/internal/testlang"
"github.com/UoY-RoboStar/rtcg/internal/testlang/rstype"
"github.com/UoY-RoboStar/rtcg/internal/testlang/value"
"github.com/UoY-RoboStar/rtcg/internal/testlang/channel"
)

// Funcs gets the C++ function map.
Expand All @@ -18,6 +20,7 @@ func Funcs() template.FuncMap {
"cppChannelMsgType": ChannelMsgType,
"cppChannelValueType": ChannelValueType,
"cppChannelTopicName": ChannelTopicName,
"cppOtherOutChannels": OtherOutChannels,
"cppConvertFrom": ConvertFrom,
"cppConvertTo": ConvertTo,
"cppEnumField": EnumField,
Expand Down Expand Up @@ -136,3 +139,27 @@ const (
testEnumName = "Test" // testEnumName is the name in the C++ code for the test enum.
outcomeEnumName = "Outcome" // outcomeEnumName is the name in the C++ code for the outcome enum.
)

func OtherOutChannels(tagg []transition.AggregateSet, channels []channel.Channel) []channel.Channel {

var carray []channel.Channel

for _, c := range channels {

if (c.IsOut()) {
var inside = false

for _, t := range tagg {
if t.Channel == c {
inside = true
}
}

if !inside {
carray = append(carray, c)
}
}
}

return carray
}

0 comments on commit e135735

Please sign in to comment.