From d60331c6239477660494466071be2e607e11e1e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergi=20Hern=C3=A1ndez?= <shernand@iri.upc.edu> Date: Sun, 6 Apr 2014 21:17:52 +0000 Subject: [PATCH] Added classes to handle and hold motion file data. --- src/CMakeLists.txt | 6 +- src/bioloid_motion_pages.cpp | 38 ++++++- src/parser/CMakeLists.txt | 4 +- src/parser/mtn_file.cpp | 154 ++++++++++++++++++++++++++++ src/parser/mtn_file.h | 36 +++++++ src/parser/mtn_file.y | 84 ++++++++++------ src/parser/mtn_file_parser.cpp | 37 +++++++ src/parser/mtn_file_parser.hpp | 11 +- src/parser/mtn_page.cpp | 178 +++++++++++++++++++++++++++++++++ src/parser/mtn_page.h | 42 ++++++++ src/parser/mtn_step.cpp | 86 ++++++++++++++++ src/parser/mtn_step.h | 25 +++++ 12 files changed, 664 insertions(+), 37 deletions(-) create mode 100644 src/parser/mtn_file.cpp create mode 100644 src/parser/mtn_file.h create mode 100644 src/parser/mtn_page.cpp create mode 100644 src/parser/mtn_page.h create mode 100644 src/parser/mtn_step.cpp create mode 100644 src/parser/mtn_step.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3279187..4d10d16 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,11 +5,11 @@ INCLUDE_DIRECTORIES(. ./parser) INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}/parser) INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}) # application source files -SET(sources bioloid_motion_pages.cpp ${motion_parser_SRCS}) +SET(sources bioloid_motion_pages.cpp) # application header files SET(headers bioloid_motion_pages.h) # create the executable file ADD_EXECUTABLE(bioloid_motion_pages ${sources}) -TARGET_LINK_LIBRARIES(bioloid_motion_pages motion_parser) +TARGET_LINK_LIBRARIES(bioloid_motion_pages mtn_parser) # link necessary libraries -ADD_DEPENDENCIES(bioloid_motion_pages motion_parser) +ADD_DEPENDENCIES(bioloid_motion_pages mtn_parser) diff --git a/src/bioloid_motion_pages.cpp b/src/bioloid_motion_pages.cpp index 85265b8..b374f6f 100755 --- a/src/bioloid_motion_pages.cpp +++ b/src/bioloid_motion_pages.cpp @@ -1,14 +1,46 @@ #include <iostream> #include <cstdlib> -#include "mc_driver.hpp" +#include "mtn_file_parser.hpp" int main(const int argc,char *argv[]) { + std::vector<unsigned short int> angles; + unsigned int i=0,j=0,k=0; + CMtnFile *motions; + CMtnPage *page; + CMtnStep *step; + if(argc!=2) return (EXIT_FAILURE); - MC::MC_Driver driver; - driver.parse(argv[1]); + MTN::MTN_File_Parser mtn_parser; + mtn_parser.parse(argv[1]); + motions=mtn_parser.get_motions(); + + std::cout << "Num. pages: " << motions->get_num_pages() << std::endl; + for(i=0;i<motions->get_num_pages();i++) + { + page=motions->get_page(i); + std::cout << "name: " << page->get_name() << std::endl; + std::cout << "next page: " << page->get_next_page() << std::endl; + std::cout << "exit page: " << page->get_exit_page() << std::endl; + std::cout << "repetitions: " << page->get_repetitions() << std::endl; + std::cout << "speed rate: " << page->get_speed_rate() << std::endl; + std::cout << "inertial force: " << page->get_inertial() << std::endl; + std::cout << "Num. steps: " << page->get_num_steps() << std::endl; + for(j=0;j<page->get_num_steps();j++) + { + step=page->get_step(j); + std::cout << "angles: "; + angles=step->get_angles(); + for(k=0;k<angles.size();k++) + std::cout << angles[k] << ","; + std::cout << std::endl; + std::cout << "pausetime: " << step->get_pause_time() << std::endl; + std::cout << "step time: " << step->get_step_time() << std::endl; + } + + } return(EXIT_SUCCESS); } diff --git a/src/parser/CMakeLists.txt b/src/parser/CMakeLists.txt index 86ad402..2c36ec5 100644 --- a/src/parser/CMakeLists.txt +++ b/src/parser/CMakeLists.txt @@ -25,7 +25,9 @@ IF(FLEX_FOUND) ) ENDIF() +SET(mtn_SRCS mtn_file_parser.cpp mtn_file.cpp mtn_page.cpp mtn_step.cpp) + include_directories(${CMAKE_CURRENT_SOURCE_DIR}) include_directories(${CMAKE_CURRENT_BINARY_DIR}) -add_library(mtn_parser SHARED ${BisonOutput} ${FlexOutput} mtn_file_parser.cpp) +add_library(mtn_parser SHARED ${BisonOutput} ${FlexOutput} ${mtn_SRCS}) diff --git a/src/parser/mtn_file.cpp b/src/parser/mtn_file.cpp new file mode 100644 index 0000000..7fe8e0d --- /dev/null +++ b/src/parser/mtn_file.cpp @@ -0,0 +1,154 @@ +#include "mtn_file.h" + +CMtnFile::CMtnFile() +{ + this->type=""; + this->version=0.0; + this->enable.clear(); + this->motor_type.clear(); + this->pages.clear(); +} + +void CMtnFile::set_type(std::string &type) +{ + this->type=type; +} + +void CMtnFile::set_version(double version) +{ + this->version=version; +} + +void CMtnFile::set_num_servos(unsigned int num_servos) +{ + this->enable.resize(num_servos); + this->motor_type.resize(num_servos); +} + +void CMtnFile::set_enable(unsigned int servo,bool enable) +{ + if(servo>this->enable.size()) + { + /* handle errors */ + } + else + this->enable[servo]=enable; +} + +void CMtnFile::set_enables(std::vector<bool> &enables) +{ + unsigned int i=0; + + if(this->enable.size()!=enables.size()) + { + /* handle errors */ + } + else + for(i=0;i<this->enable.size();i++) + this->enable[i]=enables[i]; +} + +void CMtnFile::set_motor_type(unsigned int servo, int motor_type) +{ + if(servo>this->motor_type.size()) + { + /* handle errors */ + } + else + this->motor_type[servo]=motor_type; +} + +void CMtnFile::set_motor_types(std::vector<int> &motor_types) +{ + unsigned int i=0; + + if(this->motor_type.size()!=motor_types.size()) + { + /* handle errors */ + } + else + for(i=0;i<this->motor_type.size();i++) + this->motor_type[i]=motor_types[i]; +} + +void CMtnFile::add_page(CMtnPage *page) +{ + this->pages.push_back(page); +} + +std::string CMtnFile::get_type(void) +{ + return this->type; +} + +double CMtnFile::get_version(void) +{ + return this->version; +} + +unsigned int CMtnFile::get_num_servos(void) +{ + return this->enable.size(); +} + +bool CMtnFile::get_enable(unsigned int servo) +{ + if(servo>this->enable.size()) + { + /* handle errors */ + } + else + return this->enable[servo]; +} + +std::vector<bool> CMtnFile::get_enables(void) +{ + return this->enable; +} + +int CMtnFile::get_motor_type(unsigned int servo) +{ + if(servo>this->motor_type.size()) + { + /* handle errors */ + } + else + return this->motor_type[servo]; +} + +std::vector<int> CMtnFile::get_motor_types(void) +{ + return this->motor_type; +} + +unsigned int CMtnFile::get_num_pages(void) +{ + return this->pages.size(); +} + +CMtnPage *CMtnFile::get_page(unsigned int page) +{ + if(page>this->pages.size()) + { + /* handle errors */ + } + else + return this->pages[page]; +} + +CMtnFile::~CMtnFile() +{ + unsigned int i=0; + + this->type=""; + this->version=0.0; + this->enable.clear(); + this->motor_type.clear(); + for(i=0;i<this->pages.size();i++) + { + delete this->pages[i]; + this->pages[i]=NULL; + } + this->pages.clear(); +} + diff --git a/src/parser/mtn_file.h b/src/parser/mtn_file.h new file mode 100644 index 0000000..edd2f55 --- /dev/null +++ b/src/parser/mtn_file.h @@ -0,0 +1,36 @@ +#ifndef _MTN_FILE_H +#define _MTN_FILE_H + +#include "mtn_page.h" + +class CMtnFile +{ + private: + std::string type; + double version; + std::vector<bool> enable; + std::vector<int> motor_type; + std::vector<CMtnPage *> pages; + public: + CMtnFile(); + void set_type(std::string &type); + void set_version(double version); + void set_num_servos(unsigned int num_servos); + void set_enable(unsigned int servo,bool enable); + void set_enables(std::vector<bool> &enables); + void set_motor_type(unsigned int servo, int motor_type); + void set_motor_types(std::vector<int> &motor_types); + void add_page(CMtnPage *page); + std::string get_type(void); + double get_version(void); + unsigned int get_num_servos(void); + bool get_enable(unsigned int servo); + std::vector<bool> get_enables(void); + int get_motor_type(unsigned int servo); + std::vector<int> get_motor_types(void); + unsigned int get_num_pages(void); + CMtnPage *get_page(unsigned int page); + ~CMtnFile(); +}; + +#endif diff --git a/src/parser/mtn_file.y b/src/parser/mtn_file.y index 1f65574..020d8f9 100644 --- a/src/parser/mtn_file.y +++ b/src/parser/mtn_file.y @@ -19,7 +19,7 @@ %lex-param { MTN_File_Parser &parser } %parse-param { MTN_File_Parser &parser } - + %code { #include <iostream> @@ -27,6 +27,17 @@ #include <fstream> /* include for all driver functions */ #include "mtn_file_parser.hpp" + + /* motion files variables */ + int num_servos=0; + std::vector<bool> enables; + std::vector<int> motor_types; + CMtnPage *page; + std::vector<unsigned int> compliances; + std::string motion_name; + CMtnStep *step; + std::vector<unsigned short int> angles; + /* this is silly, but I can't figure out a way around */ static int yylex(MTN::MTN_Parser::semantic_type *yylval,MTN::MTN_File_Scanner &scanner,MTN::MTN_File_Parser &parser); } @@ -52,68 +63,83 @@ motion_file: type version enables motor_types pages ; -type: TYPE EQUAL STRING { std::cout << "Type: " << *($3) << std::endl; } +type: TYPE EQUAL STRING { parser.set_type(*($3)); } ; -version: VERSION EQUAL FLOAT { std::cout << "version: " << $3 << std::endl; } +version: VERSION EQUAL FLOAT { parser.set_version($3); } ; -enables: ENABLE EQUAL enable +enables: ENABLE EQUAL enable { parser.set_num_servos(num_servos); + parser.set_enables(enables); + std::cout << num_servos << std::endl;} ; -enable: enable INT - | INT +enable: enable INT { num_servos++; + enables.push_back($2); } + | INT { num_servos++; + enables.push_back($1); } ; motor_types: - | MOTOR_TYPE EQUAL motor_type + | MOTOR_TYPE EQUAL motor_type { parser.set_motor_types(motor_types); } ; -motor_type: motor_type INT - | INT +motor_type: motor_type INT { motor_types.push_back($2); } + | INT { motor_types.push_back($1); } ; pages: pages page - | page + | page ; -page: BEGIN_PAGE name compliances params steps END_PAGE - | BEGIN_PAGE name compliances params END_PAGE +page: BEGIN_PAGE name compliances params steps END_PAGE { parser.add_page(page); + motion_name=""; + compliances.clear(); } + | BEGIN_PAGE name compliances params END_PAGE { parser.add_page(page); + motion_name=""; + compliances.clear(); } ; -name: NAME EQUAL - | NAME EQUAL strings +name: NAME EQUAL { page=new CMtnPage(num_servos); + motion_name=""; + page->set_name(motion_name); } + | NAME EQUAL strings { page=new CMtnPage(num_servos); + page->set_name(motion_name); } ; -strings: strings STRING - | STRING +strings: strings STRING { motion_name+=" "+*($2); } + | STRING { motion_name+=*($1); } ; -compliances: COMPLIANCE EQUAL compliance +compliances: COMPLIANCE EQUAL compliance { page->set_compliances(compliances); } ; -compliance: compliance INT - | INT +compliance: compliance INT { compliances.push_back($2); } + | INT { compliances.push_back($1); } ; -params: PLAY_PARAMS EQUAL INT INT INT FLOAT INT { std::cout << "next motion: " << $3 << std::endl; - std::cout << "exit motion: " << $4 << std::endl; - std::cout << "repetitions: " << $5 << std::endl; - std::cout << "speed rate: " << $6 << std::endl; - std::cout << "inertial force: " << $7 << std::endl; } +params: PLAY_PARAMS EQUAL INT INT INT FLOAT INT { page->set_next_page($3); + page->set_exit_page($4); + page->set_repetitions($5); + page->set_speed_rate($6); + page->set_inertial($7); } ; -steps: steps step +steps: steps step | step ; -step: STEP EQUAL angles FLOAT FLOAT { std::cout << "pause time: " << $4 << std::endl; - std::cout << "step time: " << $5 << std::endl; } +step: STEP EQUAL angles FLOAT FLOAT { step=new CMtnStep(num_servos); + step->set_angles(angles); + angles.clear(); + step->set_pause_time($4); + step->set_step_time($5); + page->add_step(step); } ; -angles: angles INT - | INT +angles: angles INT { angles.push_back($2); } + | INT { angles.push_back($1); } ; %% void diff --git a/src/parser/mtn_file_parser.cpp b/src/parser/mtn_file_parser.cpp index e411ec9..7d33019 100644 --- a/src/parser/mtn_file_parser.cpp +++ b/src/parser/mtn_file_parser.cpp @@ -3,6 +3,7 @@ #include <cassert> #include "mtn_file_parser.hpp" +#include "mtn_parser.hpp" MTN::MTN_File_Parser::MTN_File_Parser() { @@ -47,9 +48,45 @@ void MTN::MTN_File_Parser::parse(const char *filename) std::cerr << "Failed to allocate parser: (" << ba.what() << "), exiting!!\n"; exit( EXIT_FAILURE ); } + const int accept(0); if(parser->parse()!=accept) { std::cerr << "Parse failed!!\n"; } } + +void MTN::MTN_File_Parser::set_type(std::string &type) +{ + this->motions.set_type(type); +} + +void MTN::MTN_File_Parser::set_version(double version) +{ + this->motions.set_version(version); +} + +void MTN::MTN_File_Parser::set_num_servos(unsigned int num_servos) +{ + this->motions.set_num_servos(num_servos); +} + +void MTN::MTN_File_Parser::set_enables(std::vector<bool> &enables) +{ + this->motions.set_enables(enables); +} + +void MTN::MTN_File_Parser::set_motor_types(std::vector<int> &motor_types) +{ + this->motions.set_motor_types(motor_types); +} + +void MTN::MTN_File_Parser::add_page(CMtnPage *page) +{ + this->motions.add_page(page); +} + +CMtnFile *MTN::MTN_File_Parser::get_motions(void) +{ + return &(this->motions); +} diff --git a/src/parser/mtn_file_parser.hpp b/src/parser/mtn_file_parser.hpp index ebc4c1c..5768ba6 100644 --- a/src/parser/mtn_file_parser.hpp +++ b/src/parser/mtn_file_parser.hpp @@ -3,6 +3,7 @@ #include <string> #include "mtn_file_scanner.hpp" +#include "mtn_file.h" namespace MTN { @@ -12,10 +13,18 @@ namespace MTN MTN_File_Parser(); void parse(const char *filename); virtual ~MTN_File_Parser(); - + // motion files functions + void set_type(std::string &type); + void set_version(double version); + void set_num_servos(unsigned int num_servos); + void set_enables(std::vector<bool> &enables); + void set_motor_types(std::vector<int> &motor_types); + void add_page(CMtnPage *page); + CMtnFile *get_motions(void); private: MTN::MTN_Parser *parser; MTN::MTN_File_Scanner *scanner; + CMtnFile motions; }; } diff --git a/src/parser/mtn_page.cpp b/src/parser/mtn_page.cpp new file mode 100644 index 0000000..ea95321 --- /dev/null +++ b/src/parser/mtn_page.cpp @@ -0,0 +1,178 @@ +#include "mtn_page.h" + +CMtnPage::CMtnPage(unsigned int num_servos) +{ + this->name=""; + this->compliance.resize(num_servos); + this->next_page=-1; + this->exit_page=-1; + this->repetitions=0; + this->speed_rate=0; + this->inertial=-1; + this->steps.clear(); +} + +void CMtnPage::set_name(std::string &name) +{ + this->name=name; +} + +void CMtnPage::set_compliance(unsigned int servo,unsigned int compliance) +{ + if(servo>this->compliance.size()) + { + /* handle errors */ + } + else + this->compliance[servo]=compliance; +} + +void CMtnPage::set_compliances(std::vector<unsigned int> compliances) +{ + unsigned int i=0; + + if(this->compliance.size()!=compliances.size()) + { + /* handle errors */ + } + else + { + for(i=0;i<this->compliance.size();i++) + this->compliance[i]=compliances[i]; + } +} + +void CMtnPage::set_next_page(unsigned int page) +{ + if(page>=256) + { + /* handle errors */ + } + else + this->next_page=page; +} + +void CMtnPage::set_exit_page(unsigned int page) +{ + if(page>=256) + { + /* handle errors */ + } + else + this->exit_page=page; +} + +void CMtnPage::set_repetitions(unsigned int num) +{ + if(num>=256) + { + /* handle errors */ + } + else + this->repetitions=num; +} + +void CMtnPage::set_speed_rate(double rate) +{ + this->speed_rate=rate; +} + +void CMtnPage::set_inertial(unsigned int inertia) +{ + if(inertia>=256) + { + /* handle errors */ + } + else + this->inertial=inertia; +} + +void CMtnPage::add_step(CMtnStep *step) +{ + if(this->steps.size()>=7) + { + /* handle errors */ + } + else + this->steps.push_back(step); +} + +std::string CMtnPage::get_name(void) +{ + return this->name; +} + +unsigned int CMtnPage::get_compliance(unsigned int servo) +{ + if(servo>this->compliance.size()) + { + /* handle errors */ + } + else + return this->compliance[servo]; +} + +std::vector<unsigned int> CMtnPage::get_compliances(void) +{ + return this->compliance; +} + +unsigned int CMtnPage::get_next_page(void) +{ + return this->next_page; +} + +unsigned int CMtnPage::get_exit_page(void) +{ + return this->exit_page; +} + +unsigned int CMtnPage::get_repetitions(void) +{ + return this->repetitions; +} + +double CMtnPage::get_speed_rate(void) +{ + return this->speed_rate; +} + +unsigned int CMtnPage::get_inertial(void) +{ + return this->inertial; +} + +unsigned int CMtnPage::get_num_steps(void) +{ + return this->steps.size(); +} + +CMtnStep *CMtnPage::get_step(unsigned int step) +{ + if(step>this->steps.size()) + { + /* handle error */ + } + else + return this->steps[step]; +} + +CMtnPage::~CMtnPage() +{ + unsigned int i=0; + + this->name.clear(); + this->compliance.clear(); + this->next_page=-1; + this->exit_page=-1; + this->repetitions=0; + this->speed_rate=0; + this->inertial=-1; + for(i=0;i<this->steps.size();i++) + { + delete this->steps[i]; + this->steps[i]=NULL; + } + this->steps.clear(); +} + diff --git a/src/parser/mtn_page.h b/src/parser/mtn_page.h new file mode 100644 index 0000000..22549e1 --- /dev/null +++ b/src/parser/mtn_page.h @@ -0,0 +1,42 @@ +#ifndef _MTN_PAGE_H +#define _MTN_PAGE_H + +#include "mtn_step.h" +#include <string> + +class CMtnPage +{ + private: + std::string name; + std::vector<unsigned int> compliance; + unsigned int next_page; + unsigned int exit_page; + unsigned int repetitions; + double speed_rate; + unsigned int inertial; + std::vector<CMtnStep *> steps; + public: + CMtnPage(unsigned int num_servos); + void set_name(std::string &name); + void set_compliance(unsigned int servo,unsigned int compliance); + void set_compliances(std::vector<unsigned int> compliances); + void set_next_page(unsigned int page); + void set_exit_page(unsigned int page); + void set_repetitions(unsigned int num); + void set_speed_rate(double rate); + void set_inertial(unsigned int inertia); + void add_step(CMtnStep *step); + std::string get_name(void); + unsigned int get_compliance(unsigned int servo); + std::vector<unsigned int> get_compliances(void); + unsigned int get_next_page(void); + unsigned int get_exit_page(void); + unsigned int get_repetitions(void); + double get_speed_rate(void); + unsigned int get_inertial(void); + unsigned int get_num_steps(void); + CMtnStep *get_step(unsigned int step); + ~CMtnPage(); +}; + +#endif diff --git a/src/parser/mtn_step.cpp b/src/parser/mtn_step.cpp new file mode 100644 index 0000000..377cf35 --- /dev/null +++ b/src/parser/mtn_step.cpp @@ -0,0 +1,86 @@ +#include "mtn_step.h" + +CMtnStep::CMtnStep(unsigned int num_servos) +{ + this->angles.resize(num_servos); + this->pause_time=0.0; + this->step_time=0.0; +} + +void CMtnStep::set_angle(unsigned int servo,unsigned short int angle) +{ + if(servo>this->angles.size()) + { + /* handle errors */ + } + else + this->angles[servo]=angle; +} + +void CMtnStep::set_angles(std::vector<unsigned short int> &angles) +{ + unsigned int i=0; + + if(angles.size()!=this->angles.size()) + { + /* handle errors */ + } + else + { + for(i=0;i<angles.size();i++) + this->angles[i]=angles[i]; + } +} + +void CMtnStep::set_pause_time(double time) +{ + if(time<0.0) + { + /* handle errors */ + } + else + this->pause_time=time; +} + +void CMtnStep::set_step_time(double time) +{ + if(time<0.0) + { + /* handle errors */ + } + else + this->step_time=time; +} + +unsigned short int CMtnStep::get_angle(unsigned int servo) +{ + if(servo>this->angles.size()) + { + /* handler errors */ + } + else + return this->angles[servo]; +} + +std::vector<unsigned short int> CMtnStep::get_angles(void) +{ + return this->angles; +} + +double CMtnStep::get_pause_time(void) +{ + return this->pause_time; +} + +double CMtnStep::get_step_time(void) +{ + return this->step_time; +} + +CMtnStep::~CMtnStep() +{ + this->angles.clear(); + this->pause_time=0.0; + this->step_time=0.0; +} + diff --git a/src/parser/mtn_step.h b/src/parser/mtn_step.h new file mode 100644 index 0000000..f85b4f1 --- /dev/null +++ b/src/parser/mtn_step.h @@ -0,0 +1,25 @@ +#ifndef _MTN_STEP_H +#define _MTN_STEP_H + +#include <vector> + +class CMtnStep +{ + private: + std::vector<unsigned short int> angles; + double pause_time; + double step_time; + public: + CMtnStep(unsigned int num_servos); + void set_angle(unsigned int servo,unsigned short int angle); + void set_angles(std::vector<unsigned short int> &angles); + void set_pause_time(double time); + void set_step_time(double time); + unsigned short int get_angle(unsigned int servo); + std::vector<unsigned short int> get_angles(void); + double get_pause_time(void); + double get_step_time(void); + ~CMtnStep(); +}; + +#endif -- GitLab