Skip to content

Commit 1d8c934

Browse files
authored
Protobuf Formatting and Style Guide (UBC-Thunderbots#1285)
* Add proto support to clang format and format all proto files * Protobuf style guide * Fix build errors
1 parent 605b536 commit 1d8c934

17 files changed

+408
-302
lines changed

.clang-format

+25-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,29 @@ Language: JavaScript
3131
DisableFormat: true
3232
---
3333
Language: Proto
34-
# Don't format proto files.
35-
DisableFormat: true
34+
BasedOnStyle: Google
35+
AlignAfterOpenBracket: Align
36+
AlignConsecutiveAssignments: 'true'
37+
AlignEscapedNewlinesLeft: 'false'
38+
AllowShortBlocksOnASingleLine: 'false'
39+
AllowShortCaseLabelsOnASingleLine: 'false'
40+
AllowShortFunctionsOnASingleLine: Empty
41+
AllowShortIfStatementsOnASingleLine: 'false'
42+
AllowShortLoopsOnASingleLine: 'false'
43+
BinPackArguments: 'true'
44+
BinPackParameters: 'true'
45+
BreakBeforeBraces: Allman
46+
BreakStringLiterals: 'false'
47+
ColumnLimit: '90'
48+
Cpp11BracedListStyle: 'true'
49+
IncludeBlocks: Regroup
50+
IndentCaseLabels: 'true'
51+
IndentWidth: '4'
52+
MaxEmptyLinesToKeep: '3'
53+
NamespaceIndentation: All
54+
ReflowComments: 'true'
55+
SortIncludes: 'true'
56+
Standard: Cpp11
57+
TabWidth: '4'
58+
UseTab: Never
3659
---

docs/code-style-guide.md

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* [Getter And Setter Functions](#getter-and-setter-functions)
1313
* [Spelling](#spelling)
1414
* [Miscellaneous](#miscellaneous)
15+
* [Protobuf](#protobuf)
1516

1617

1718
Our C++ coding style is based off of [Google's C++ Style Guide](https://google.github.io/styleguide/cppguide.html). We use [clang-format](https://clang.llvm.org/docs/ClangFormat.html) to enforce most of the nit-picky parts of the style, such as brackets and alignment, so this document highlights the important rules to follow that clang-format cannot enforce.
@@ -315,3 +316,6 @@ Some general guidelines when writing tests are:
315316
c[i] = i + 1;
316317
```
317318

319+
### Protobuf
320+
321+
Protobufs that we define should follow [Google's Protobuf Style Guide](https://developers.google.com/protocol-buffers/docs/style).

formatting_scripts/fix_formatting.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ CURR_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
1414
BAZEL_ROOT_DIR="$CURR_DIR/../src"
1515

1616
# Extensions to check formatting for clang-format
17-
CLANG_FORMAT_EXTENSIONS=(h cpp c hpp tpp)
17+
CLANG_FORMAT_EXTENSIONS=(h cpp c hpp tpp proto)
1818

1919
# Function to run clang format
2020
function run_clang_format () {

src/firmware_new/boards/frankie_v1/udp_multicast.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
// TODO the messages are global for now, will need to be moved
1919
// when this file is properly integrated. This file only serves
2020
// as a reference implementation of reliable multicast with IPv6
21-
robot_ack ack = robot_ack_init_zero;
22-
control_msg control = control_msg_init_zero;
21+
RobotAck ack = RobotAck_init_zero;
22+
ControlMsg control = ControlMsg_init_zero;
2323

2424
/*
2525
* Thread that creates a send and recv socket, joins the specified
@@ -71,7 +71,7 @@ static void blocking_udp_multicast_loop(void *arg)
7171
netconn_join_leave_group(recvconn, &config->multicast_address, NULL, NETCONN_JOIN);
7272

7373
// this buffer is used to hold serialized proto
74-
uint8_t buffer[robot_ack_size];
74+
uint8_t buffer[RobotAck_size];
7575

7676
while (1)
7777
{
@@ -80,20 +80,20 @@ static void blocking_udp_multicast_loop(void *arg)
8080
if (err == ERR_OK)
8181
{
8282
tx_buf = netbuf_new();
83-
netbuf_alloc(tx_buf, robot_ack_size);
83+
netbuf_alloc(tx_buf, RobotAck_size);
8484

8585
// Create a stream that reads from the buffer
8686
pb_istream_t in_stream =
8787
pb_istream_from_buffer((uint8_t *)rx_buf->p->payload, rx_buf->p->tot_len);
8888

89-
if (pb_decode(&in_stream, control_msg_fields, &control))
89+
if (pb_decode(&in_stream, ControlMsg_fields, &control))
9090
{
9191
// update proto
9292
ack.msg_count = msg_count++;
9393

9494
// serialize proto
9595
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
96-
pb_encode(&stream, robot_ack_fields, &ack);
96+
pb_encode(&stream, RobotAck_fields, &ack);
9797

9898
// package payload and send over udp
9999
tx_buf->p->payload = buffer;

src/firmware_new/proto/control.proto

+10-10
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@ syntax = "proto2";
33
// A test robot msg which serves as an example to modify and use to
44
// facilitate firmware testing.
55

6-
message control_msg {
7-
8-
required wheel_speed_msg wheel_1_control = 1;
9-
optional wheel_speed_msg wheel_2_control = 2;
10-
optional wheel_speed_msg wheel_3_control = 3;
11-
optional wheel_speed_msg wheel_4_control = 4;
12-
6+
message ControlMsg
7+
{
8+
required WheelSpeedMsg wheel_1_control = 1;
9+
optional WheelSpeedMsg wheel_2_control = 2;
10+
optional WheelSpeedMsg wheel_3_control = 3;
11+
optional WheelSpeedMsg wheel_4_control = 4;
1312
}
1413

15-
message wheel_speed_msg {
16-
14+
message WheelSpeedMsg
15+
{
1716
// speed at which to rotate
1817
required uint32 rpm = 1;
1918

@@ -22,6 +21,7 @@ message wheel_speed_msg {
2221
}
2322

2423
// Robot response
25-
message robot_ack {
24+
message RobotAck
25+
{
2626
required uint32 msg_count = 1;
2727
}

src/firmware_new/tools/communication/robot_communicator.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,4 @@ void RobotCommunicator<SendProto, ReceiveProto>::send_loop(
7777
}
7878

7979
// place all templated communcation msg send/receive pair initializations here
80-
template class RobotCommunicator<control_msg, robot_ack>;
80+
template class RobotCommunicator<ControlMsg, RobotAck>;

src/firmware_new/tools/send_proto_over_udp.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,27 @@ int main(int argc, char* argv[])
2222

2323
// we create a wheel control msg, and request wheel 1 to spin at 100 rpm forwards
2424
// these wheel profile will be used across multiple wheels
25-
wheel_speed_msg wheel_control;
26-
wheel_control.set_rpm(69);
27-
wheel_control.set_forwards(true);
25+
WheelSpeedMsg WheelControl;
26+
WheelControl.set_rpm(69);
27+
WheelControl.set_forwards(true);
2828

2929
// turn two of the wheels on with this profile
3030
// NOTE that the other two wheels are not being populated
31-
control_msg control_req;
32-
control_req.mutable_wheel_1_control()->CopyFrom(wheel_control);
33-
control_req.mutable_wheel_2_control()->CopyFrom(wheel_control);
34-
control_req.mutable_wheel_2_control()->CopyFrom(wheel_control);
31+
ControlMsg control_req;
32+
control_req.mutable_wheel_1_control()->CopyFrom(WheelControl);
33+
control_req.mutable_wheel_2_control()->CopyFrom(WheelControl);
34+
control_req.mutable_wheel_2_control()->CopyFrom(WheelControl);
3535

3636
int count = 0;
3737

3838
// create a RobotCommunicator with a NetworkMedium
39-
RobotCommunicator<control_msg, robot_ack> communicator(
39+
RobotCommunicator<ControlMsg, RobotAck> communicator(
4040
std::make_unique<NetworkMedium>(std::string(AI_MULTICAST_ADDRESS) + "%eth0",
4141
AI_MULTICAST_SEND_PORT, AI_UNICAST_LISTEN_PORT),
42-
[&](const control_msg& msg) {
42+
[&](const ControlMsg& msg) {
4343
std::cout << "COMP Txed " << count++ << " msgs " << std::endl;
4444
},
45-
[&](const robot_ack& msg) {
45+
[&](const RobotAck& msg) {
4646
std::cout << "STM32 Rxed " << msg.msg_count() << " msgs " << std::endl;
4747
});
4848

+19-19
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
syntax = "proto2";
22

3-
message grSim_Robot_Command {
4-
required uint32 id = 1;
5-
required float kickspeedx = 2;
6-
required float kickspeedz = 3;
7-
required float veltangent = 4;
8-
required float velnormal = 5;
9-
required float velangular = 6;
10-
required bool spinner = 7;
11-
required bool wheelsspeed = 8;
12-
optional float wheel1 = 9;
13-
optional float wheel2 = 10;
14-
optional float wheel3 = 11;
15-
optional float wheel4 = 12;
3+
message grSim_Robot_Command
4+
{
5+
required uint32 id = 1;
6+
required float kickspeedx = 2;
7+
required float kickspeedz = 3;
8+
required float veltangent = 4;
9+
required float velnormal = 5;
10+
required float velangular = 6;
11+
required bool spinner = 7;
12+
required bool wheelsspeed = 8;
13+
optional float wheel1 = 9;
14+
optional float wheel2 = 10;
15+
optional float wheel3 = 11;
16+
optional float wheel4 = 12;
1617
}
1718

18-
message grSim_Commands {
19-
required double timestamp = 1;
20-
required bool isteamyellow = 2;
21-
repeated grSim_Robot_Command robot_commands = 3;
19+
message grSim_Commands
20+
{
21+
required double timestamp = 1;
22+
required bool isteamyellow = 2;
23+
repeated grSim_Robot_Command robot_commands = 3;
2224
}
23-
24-

src/software/proto/grSim_Packet.proto

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
syntax = "proto2";
22
import "software/proto/grSim_Commands.proto";
33
import "software/proto/grSim_Replacement.proto";
4-
message grSim_Packet {
5-
optional grSim_Commands commands = 1;
4+
message grSim_Packet
5+
{
6+
optional grSim_Commands commands = 1;
67
optional grSim_Replacement replacement = 2;
78
}
+18-15
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
syntax = "proto2";
2-
message grSim_RobotReplacement {
3-
required double x=1;
4-
required double y=2;
5-
required double dir=3;
6-
required uint32 id=4;
7-
required bool yellowteam=5;
8-
optional bool turnon=6;
2+
message grSim_RobotReplacement
3+
{
4+
required double x = 1;
5+
required double y = 2;
6+
required double dir = 3;
7+
required uint32 id = 4;
8+
required bool yellowteam = 5;
9+
optional bool turnon = 6;
910
}
1011

11-
message grSim_BallReplacement {
12-
required double x=1;
13-
required double y=2;
14-
required double vx=3;
15-
required double vy=4;
12+
message grSim_BallReplacement
13+
{
14+
required double x = 1;
15+
required double y = 2;
16+
required double vx = 3;
17+
required double vy = 4;
1618
}
1719

18-
message grSim_Replacement {
19-
optional grSim_BallReplacement ball = 1;
20-
repeated grSim_RobotReplacement robots = 2;
20+
message grSim_Replacement
21+
{
22+
optional grSim_BallReplacement ball = 1;
23+
repeated grSim_RobotReplacement robots = 2;
2124
}
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
11
syntax = "proto2";
22

3-
message SSL_DetectionBall {
4-
required float confidence = 1;
5-
optional uint32 area = 2;
6-
required float x = 3;
7-
required float y = 4;
8-
optional float z = 5;
9-
required float pixel_x = 6;
10-
required float pixel_y = 7;
3+
message SSL_DetectionBall
4+
{
5+
required float confidence = 1;
6+
optional uint32 area = 2;
7+
required float x = 3;
8+
required float y = 4;
9+
optional float z = 5;
10+
required float pixel_x = 6;
11+
required float pixel_y = 7;
1112
}
1213

13-
message SSL_DetectionRobot {
14-
required float confidence = 1;
15-
optional uint32 robot_id = 2;
16-
required float x = 3;
17-
required float y = 4;
18-
optional float orientation = 5;
19-
required float pixel_x = 6;
20-
required float pixel_y = 7;
21-
optional float height = 8;
14+
message SSL_DetectionRobot
15+
{
16+
required float confidence = 1;
17+
optional uint32 robot_id = 2;
18+
required float x = 3;
19+
required float y = 4;
20+
optional float orientation = 5;
21+
required float pixel_x = 6;
22+
required float pixel_y = 7;
23+
optional float height = 8;
2224
}
2325

24-
message SSL_DetectionFrame {
25-
required uint32 frame_number = 1;
26-
required double t_capture = 2;
27-
required double t_sent = 3;
28-
required uint32 camera_id = 4;
29-
repeated SSL_DetectionBall balls = 5;
30-
repeated SSL_DetectionRobot robots_yellow = 6;
31-
repeated SSL_DetectionRobot robots_blue = 7;
26+
message SSL_DetectionFrame
27+
{
28+
required uint32 frame_number = 1;
29+
required double t_capture = 2;
30+
required double t_sent = 3;
31+
required uint32 camera_id = 4;
32+
repeated SSL_DetectionBall balls = 5;
33+
repeated SSL_DetectionRobot robots_yellow = 6;
34+
repeated SSL_DetectionRobot robots_blue = 7;
3235
}

0 commit comments

Comments
 (0)