Controlor for Humanoid. Walking trajectory generator, sensor reflection etc.
Dependencies: Adafruit-PWM-Servo-Driver MPU6050 RS300 mbed
CSV.cpp@12:6cd135bf03bd, 2013-02-03 (annotated)
- Committer:
- syundo0730
- Date:
- Sun Feb 03 04:53:44 2013 +0000
- Revision:
- 12:6cd135bf03bd
- Child:
- 13:711f74b2fa33
file reading, motion selecting, serial communication
Who changed what in which revision?
User | Revision | Line number | New 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 | stringstream sstr; |
syundo0730 | 12:6cd135bf03bd | 23 | |
syundo0730 | 12:6cd135bf03bd | 24 | if (str_line[0] == "servo_size") { |
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 | 12:6cd135bf03bd | 28 | sstr << str_line[1]; |
syundo0730 | 12:6cd135bf03bd | 29 | sstr >> *motion_size; |
syundo0730 | 12:6cd135bf03bd | 30 | } else if (str_line[0] == "pose_size") { |
syundo0730 | 12:6cd135bf03bd | 31 | sstr << str_line[1]; |
syundo0730 | 12:6cd135bf03bd | 32 | sstr >> pose_size[id]; |
syundo0730 | 12:6cd135bf03bd | 33 | ++ id; |
syundo0730 | 12:6cd135bf03bd | 34 | } else { |
syundo0730 | 12:6cd135bf03bd | 35 | int size = str_line.size(); |
syundo0730 | 12:6cd135bf03bd | 36 | for(int i = 0; i < size; ++i) { |
syundo0730 | 12:6cd135bf03bd | 37 | sstr << str_line[i]; |
syundo0730 | 12:6cd135bf03bd | 38 | sstr >> p[i]; |
syundo0730 | 12:6cd135bf03bd | 39 | } |
syundo0730 | 12:6cd135bf03bd | 40 | p += size; |
syundo0730 | 12:6cd135bf03bd | 41 | } |
syundo0730 | 12:6cd135bf03bd | 42 | } |
syundo0730 | 12:6cd135bf03bd | 43 | file.close(); |
syundo0730 | 12:6cd135bf03bd | 44 | return true; |
syundo0730 | 12:6cd135bf03bd | 45 | } |
syundo0730 | 12:6cd135bf03bd | 46 | |
syundo0730 | 12:6cd135bf03bd | 47 | std::vector<string> CSV::split(string &src, string key) { |
syundo0730 | 12:6cd135bf03bd | 48 | |
syundo0730 | 12:6cd135bf03bd | 49 | string str = src; |
syundo0730 | 12:6cd135bf03bd | 50 | vector<string> str_line; |
syundo0730 | 12:6cd135bf03bd | 51 | int str_len = str.length(); |
syundo0730 | 12:6cd135bf03bd | 52 | int key_len = key.length(); |
syundo0730 | 12:6cd135bf03bd | 53 | |
syundo0730 | 12:6cd135bf03bd | 54 | int index = 0; |
syundo0730 | 12:6cd135bf03bd | 55 | while(index < str_len) { |
syundo0730 | 12:6cd135bf03bd | 56 | int oldindex = index; |
syundo0730 | 12:6cd135bf03bd | 57 | index = str.find(key, index); |
syundo0730 | 12:6cd135bf03bd | 58 | index = (index == string::npos ? str_len : index); |
syundo0730 | 12:6cd135bf03bd | 59 | string tmp = str.substr(oldindex, index - oldindex); |
syundo0730 | 12:6cd135bf03bd | 60 | str_line.push_back(tmp); |
syundo0730 | 12:6cd135bf03bd | 61 | index += key_len; |
syundo0730 | 12:6cd135bf03bd | 62 | } |
syundo0730 | 12:6cd135bf03bd | 63 | return str_line; |
syundo0730 | 12:6cd135bf03bd | 64 | } |