Skip to content
Snippets Groups Projects
Commit f8f096c0 authored by Sergi Hernandez's avatar Sergi Hernandez
Browse files

Moved most of the code to separate libraries for code reusability.

Added a file to generate the stm32 code.
parent b6854522
No related branches found
No related tags found
No related merge requests found
...@@ -2,7 +2,7 @@ ADD_SUBDIRECTORY(mtn) ...@@ -2,7 +2,7 @@ ADD_SUBDIRECTORY(mtn)
ADD_SUBDIRECTORY(bin) ADD_SUBDIRECTORY(bin)
# locate the necessary dependencies # locate the necessary dependencies
# add the necessary include directories # add the necessary include directories
INCLUDE_DIRECTORIES(. ./mtn) INCLUDE_DIRECTORIES(. ./mtn ./bin)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}/mtn) INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}/mtn)
# application source files # application source files
SET(sources bioloid_motion_pages.cpp) SET(sources bioloid_motion_pages.cpp)
......
...@@ -3,10 +3,13 @@ ...@@ -3,10 +3,13 @@
#include <string.h> #include <string.h>
#include "mtn_file_parser.hpp" #include "mtn_file_parser.hpp"
#include "bin_file_parser.h"
int main(const int argc,char *argv[]) int main(const int argc,char *argv[])
{ {
std::vector<unsigned short int> angles; std::vector<unsigned short int> angles;
MTN::MTN_File_Parser mtn_parser;
CBinFileParser bin_parser;
unsigned int i=0,j=0,k=0; unsigned int i=0,j=0,k=0;
CMtnFile *motions; CMtnFile *motions;
CMtnPage *page; CMtnPage *page;
...@@ -21,9 +24,13 @@ int main(const int argc,char *argv[]) ...@@ -21,9 +24,13 @@ int main(const int argc,char *argv[])
{ {
if(strstr(argv[1],".mtn")!=NULL) if(strstr(argv[1],".mtn")!=NULL)
{ {
mtn_parser.parse(argv[1]);
motions=mtn_parser.get_motions();
} }
else if(strstr(argv[1],".bin")!=NULL) else if(strstr(argv[1],".bin")!=NULL)
{ {
bin_parser.parse(argv[1]);
motions=bin_parser.get_motions();
} }
else else
{ {
...@@ -31,10 +38,7 @@ int main(const int argc,char *argv[]) ...@@ -31,10 +38,7 @@ int main(const int argc,char *argv[])
return (EXIT_FAILURE); return (EXIT_FAILURE);
} }
} }
MTN::MTN_File_Parser mtn_parser; /* show motion pages */
mtn_parser.parse(argv[1]);
motions=mtn_parser.get_motions();
std::cout << "Num. pages: " << motions->get_num_pages() << std::endl; std::cout << "Num. pages: " << motions->get_num_pages() << std::endl;
for(i=0;i<motions->get_num_pages();i++) for(i=0;i<motions->get_num_pages();i++)
{ {
...@@ -59,6 +63,10 @@ int main(const int argc,char *argv[]) ...@@ -59,6 +63,10 @@ int main(const int argc,char *argv[])
} }
} }
/* generate the stm32 c code for the motion pages */
std::cout << "#include \"motion_pages.h\"\n" << std::endl << std::endl;
std::cout << "TPage motion_pages[MAX_PAGES] __attribute__ ((section (\".pages\")))={\n" << std::endl;
return(EXIT_SUCCESS); return(EXIT_SUCCESS);
} }
......
#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();
}
#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
#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();
}
#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
#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;
}
#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
#ifndef _STM32_SRC_MTN_H
#define _STM32_SRC_MTN_H
#include "mtn_file.h";
class CSTM32SrcMtn
{
private:
CMtnFile *motions;
public:
CSTM32SrcMtn();
~CSTM32SrcMtn();
};
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment