Team 302 Software Training
Data Driven Robot
Data Driven Robot
Prerequisite Training:
Data Driven Robot
Activity: Write parsing code to parse a simple Robot.xml file (Thing 1 / Thing 2)
Robot.dtd - Defines Valid Definitions
Parent-Child element relationships
<!ELEMENT robot (navx?, pigeon?, pcm?, pdp?, lidar*, camera*, led?, chassis?, mechanism* )>
Robot.dtd - Defines Valid Definitions
Attribute settings
<!ATTLIST chassis
type ( 0 | 1 | 2 ) "0"
wheelDiameter CDATA #REQUIRED
wheelBase CDATA #REQUIRED
track CDATA #REQUIRED
>
Robot.dtd - Defines Valid Definitions
Robot.xml
<?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">
Robot.xml
XML Parsing Files
XML Parsing Files - MotorDefn.cpp
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 );
}
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 );
}