1 of 12

Team 302 Software Training

Data Driven Robot

2 of 12

Data Driven Robot

Prerequisite Training:

  • C++ Programming I or equivalent
  • Introduction to WPILIB/CTRE or equivalent
  • Introduction to Iterative Robot or equivalent
  • Introduction to XML or equivalent

3 of 12

Data Driven Robot

  • Robot.dtd
  • Robot.xml
  • Parsing structure

Activity: Write parsing code to parse a simple Robot.xml file (Thing 1 / Thing 2)

4 of 12

Robot.dtd - Defines Valid Definitions

Parent-Child element relationships

<!ELEMENT robot (navx?, pigeon?, pcm?, pdp?, lidar*, camera*, led?, chassis?, mechanism* )>

  • Robot can have 0 or 1:
    • navx, pigeon, pcm, pdp, led, chassis
  • Robot can have 0 or more:
    • Lidar, camera, mechanism

5 of 12

Robot.dtd - Defines Valid Definitions

Attribute settings

<!ATTLIST chassis

type ( 0 | 1 | 2 ) "0"

wheelDiameter CDATA #REQUIRED

wheelBase CDATA #REQUIRED

track CDATA #REQUIRED

>

  • Chassis has a:
  • type of 0, 1 or 2 with the default being 0
  • wheelDiameter which is required
  • wheelBase which is required
  • track which is required

6 of 12

Robot.dtd - Defines Valid Definitions

Review file: Robot.dtd

7 of 12

Robot.xml

  • Defines the actual robot definition
  • Needs to conform to the Robot.dtd definition

<?xml version="1.0"?>

<!DOCTYPE robot SYSTEM "robot.dtd">

<robot>

<!-- ====================================================

Pigeon

==================================================== -->

<pigeon canId="30" />

<!-- ====================================================

Chassis

==================================================== -->

<chassis type="0"

wheelDiameter="6.285"

wheelBase="25.0"

track="18.0">

8 of 12

Robot.xml

Review File: Robot.xml

9 of 12

XML Parsing Files

  • Each Element Type has its own XML Parsing Class (e.g. MotorDefn)
  • The XML Parsing Class has a static ParseXML method that:
    • Defines (Attribute) variables for each attribute
    • Attribute variables are initialized to the value specified in the DTD
    • Parses XML file
      • Looping through the attributes
        • Validates each attribute
        • Updates attribute variable values to parsed (XML file) value
      • Looping through the child elements
        • Calls the appropriate XML Parsing ClassName::ParseXML() method
    • If no errors, constructs the hardware component
    • Otherwise prints the errors to the log file

10 of 12

XML Parsing Files - MotorDefn.cpp

Review File: MotorDefn.cpp

11 of 12

RobotDefn::ParseXML()

void RobotDefn::ParseXML()

{

const char* filename = "/home/lvuser/config/robot.xml"; // set the file to parse

pugi::xml_document doc;

pugi::xml_parse_result result = doc.load_file( filename ); // load the xml file into memory (parse it)

if ( result ) // Parsed successfully

{

pugi::xml_node parent = doc.root(); // get the root node <robot>

for (pugi::xml_node node = parent.first_child(); node; node = node.next_sibling())

{

// loop through the direct children of <robot> and call the appropriate parser

for (pugi::xml_node child = node.first_child(); child; child = child.next_sibling())

{

if ( strcmp( child.name(), "chassis") == 0 )

{

ChassisDefn::ParseXML( child );

}

12 of 12

RobotDefn::ParseXML()

else if ( strcmp( child.name(), "mechanism") == 0 )

{

MechanismDefn::ParseXML( child );

}

else if ( strcmp( child.name(), "pcm") == 0 )

{

PCMDefn::ParseXML( child );

}

else if ( strcmp( child.name(), "pdp") == 0 )

{

PDPDefn::ParseXML( child );

}