Controlor for Humanoid. Walking trajectory generator, sensor reflection etc.

Dependencies:   Adafruit-PWM-Servo-Driver MPU6050 RS300 mbed

Committer:
syundo0730
Date:
Fri Nov 22 00:30:42 2013 +0000
Revision:
23:0927e605af4b
Parent:
13:711f74b2fa33
first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
syundo0730 12:6cd135bf03bd 1 #include <iostream>
syundo0730 12:6cd135bf03bd 2 #include <fstream>
syundo0730 12:6cd135bf03bd 3 #include <sstream>
syundo0730 12:6cd135bf03bd 4 #include <string>
syundo0730 12:6cd135bf03bd 5 #include <vector>
syundo0730 12:6cd135bf03bd 6 #include "CSV.h"
syundo0730 12:6cd135bf03bd 7
syundo0730 12:6cd135bf03bd 8 using namespace std;
syundo0730 12:6cd135bf03bd 9
syundo0730 12:6cd135bf03bd 10 bool CSV::read(string filename, uint16_t* p, int* servo_size, int* motion_size, int* pose_size) {
syundo0730 12:6cd135bf03bd 11 fstream file;
syundo0730 12:6cd135bf03bd 12 string str;
syundo0730 12:6cd135bf03bd 13 int id = 0;
syundo0730 12:6cd135bf03bd 14
syundo0730 12:6cd135bf03bd 15 file.open(filename.c_str(), ios::in);
syundo0730 12:6cd135bf03bd 16 if(! file.is_open()) {
syundo0730 12:6cd135bf03bd 17 return false;
syundo0730 12:6cd135bf03bd 18 }
syundo0730 12:6cd135bf03bd 19
syundo0730 12:6cd135bf03bd 20 while(getline(file, str)) {
syundo0730 12:6cd135bf03bd 21 vector<string> str_line = split(str, (string)",");
syundo0730 12:6cd135bf03bd 22
syundo0730 12:6cd135bf03bd 23 if (str_line[0] == "servo_size") {
syundo0730 13:711f74b2fa33 24 stringstream sstr;
syundo0730 12:6cd135bf03bd 25 sstr << str_line[1];
syundo0730 12:6cd135bf03bd 26 sstr >> *servo_size;
syundo0730 12:6cd135bf03bd 27 } else if (str_line[0] == "motion_size") {
syundo0730 13:711f74b2fa33 28 stringstream sstr;
syundo0730 12:6cd135bf03bd 29 sstr << str_line[1];
syundo0730 12:6cd135bf03bd 30 sstr >> *motion_size;
syundo0730 12:6cd135bf03bd 31 } else if (str_line[0] == "pose_size") {
syundo0730 13:711f74b2fa33 32 stringstream sstr;
syundo0730 12:6cd135bf03bd 33 sstr << str_line[1];
syundo0730 12:6cd135bf03bd 34 sstr >> pose_size[id];
syundo0730 12:6cd135bf03bd 35 ++ id;
syundo0730 12:6cd135bf03bd 36 } else {
syundo0730 12:6cd135bf03bd 37 int size = str_line.size();
syundo0730 12:6cd135bf03bd 38 for(int i = 0; i < size; ++i) {
syundo0730 13:711f74b2fa33 39 stringstream sstr;
syundo0730 12:6cd135bf03bd 40 sstr << str_line[i];
syundo0730 12:6cd135bf03bd 41 sstr >> p[i];
syundo0730 12:6cd135bf03bd 42 }
syundo0730 12:6cd135bf03bd 43 p += size;
syundo0730 12:6cd135bf03bd 44 }
syundo0730 12:6cd135bf03bd 45 }
syundo0730 12:6cd135bf03bd 46 file.close();
syundo0730 12:6cd135bf03bd 47 return true;
syundo0730 12:6cd135bf03bd 48 }
syundo0730 12:6cd135bf03bd 49
syundo0730 12:6cd135bf03bd 50 std::vector<string> CSV::split(string &src, string key) {
syundo0730 12:6cd135bf03bd 51
syundo0730 12:6cd135bf03bd 52 string str = src;
syundo0730 12:6cd135bf03bd 53 vector<string> str_line;
syundo0730 12:6cd135bf03bd 54 int str_len = str.length();
syundo0730 12:6cd135bf03bd 55 int key_len = key.length();
syundo0730 12:6cd135bf03bd 56
syundo0730 12:6cd135bf03bd 57 int index = 0;
syundo0730 12:6cd135bf03bd 58 while(index < str_len) {
syundo0730 12:6cd135bf03bd 59 int oldindex = index;
syundo0730 12:6cd135bf03bd 60 index = str.find(key, index);
syundo0730 12:6cd135bf03bd 61 index = (index == string::npos ? str_len : index);
syundo0730 12:6cd135bf03bd 62 string tmp = str.substr(oldindex, index - oldindex);
syundo0730 12:6cd135bf03bd 63 str_line.push_back(tmp);
syundo0730 12:6cd135bf03bd 64 index += key_len;
syundo0730 12:6cd135bf03bd 65 }
syundo0730 12:6cd135bf03bd 66 return str_line;
syundo0730 12:6cd135bf03bd 67 }