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

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

Committer:
syundo0730
Date:
Wed Sep 04 03:59:40 2013 +0000
Revision:
21:a54bcab078ed
Parent:
20:abb7852df747
Child:
22:bf5aa20b9df0
Motion handling was moved to out of motion class

Who changed what in which revision?

UserRevisionLine numberNew contents of line
syundo0730 12:6cd135bf03bd 1 #include <iostream>
syundo0730 12:6cd135bf03bd 2 #include <string>
syundo0730 17:60de3bfdc70b 3
syundo0730 20:abb7852df747 4 #include "Controlor.h"
syundo0730 12:6cd135bf03bd 5
syundo0730 17:60de3bfdc70b 6 Ticker tick;
syundo0730 12:6cd135bf03bd 7
syundo0730 21:a54bcab078ed 8 const float TIMESTEP = 0.01;
syundo0730 21:a54bcab078ed 9
syundo0730 21:a54bcab078ed 10
syundo0730 20:abb7852df747 11 //detach may be better to controled from Controlor class
syundo0730 13:711f74b2fa33 12
syundo0730 20:abb7852df747 13 Controlor::Controlor(uint16_t* data) : playing(false), attached(false)
syundo0730 12:6cd135bf03bd 14 {
syundo0730 21:a54bcab078ed 15 //pwm = new PWM();
syundo0730 21:a54bcab078ed 16 pwm = new Adafruit_PWMServoDriver(p9, p10);
syundo0730 21:a54bcab078ed 17 pwm->begin();
syundo0730 21:a54bcab078ed 18 pwm->setPrescale(64); //This value is decided for 10ms interval.
syundo0730 21:a54bcab078ed 19 pwm->setI2Cfreq(400000); //400kHz
syundo0730 21:a54bcab078ed 20
syundo0730 17:60de3bfdc70b 21 //comu = new SCI(p28, p27);
syundo0730 17:60de3bfdc70b 22 comu = new SCI(USBTX, USBRX);
syundo0730 17:60de3bfdc70b 23
syundo0730 12:6cd135bf03bd 24 LocalFileSystem* local = new LocalFileSystem("local");
syundo0730 17:60de3bfdc70b 25
syundo0730 12:6cd135bf03bd 26 read("/local/motion.csv", data);
syundo0730 12:6cd135bf03bd 27 set(data);
syundo0730 17:60de3bfdc70b 28
syundo0730 17:60de3bfdc70b 29 // Motion 0 is Home position
syundo0730 17:60de3bfdc70b 30 setmotion(0);
syundo0730 12:6cd135bf03bd 31 }
syundo0730 12:6cd135bf03bd 32
syundo0730 20:abb7852df747 33 Controlor::~Controlor()
syundo0730 12:6cd135bf03bd 34 {
syundo0730 12:6cd135bf03bd 35 for (int i = 0; i < motion_size; i++) {
syundo0730 12:6cd135bf03bd 36 delete[] motions[i];
syundo0730 12:6cd135bf03bd 37 }
syundo0730 12:6cd135bf03bd 38 delete[] motions;
syundo0730 16:e65c192b7ecf 39
syundo0730 16:e65c192b7ecf 40 delete comu;
syundo0730 16:e65c192b7ecf 41 delete pwm;
syundo0730 12:6cd135bf03bd 42 }
syundo0730 12:6cd135bf03bd 43
syundo0730 20:abb7852df747 44 void Controlor::read(const string& filename, uint16_t* data)
syundo0730 12:6cd135bf03bd 45 {
syundo0730 12:6cd135bf03bd 46 CSV csv;
syundo0730 20:abb7852df747 47 pose_size = new int;//<-This code is suspicious
syundo0730 13:711f74b2fa33 48 csv.read(filename, data, &servo_size, &motion_size, pose_size);
syundo0730 19:c2ec475367aa 49 //readMotion(filename, data, servo_size, motion_size, pose_size); // not so good at speed and has bug in handling float motion value.
syundo0730 12:6cd135bf03bd 50 }
syundo0730 12:6cd135bf03bd 51
syundo0730 20:abb7852df747 52 void Controlor::set(uint16_t* data)
syundo0730 12:6cd135bf03bd 53 {
syundo0730 20:abb7852df747 54 int size_z = motion_size;
syundo0730 20:abb7852df747 55 int size_x = servo_size;
syundo0730 12:6cd135bf03bd 56
syundo0730 13:711f74b2fa33 57 motions = new uint16_t**[size_z];
syundo0730 12:6cd135bf03bd 58 uint16_t* p = data;
syundo0730 12:6cd135bf03bd 59
syundo0730 12:6cd135bf03bd 60 for (int i = 0; i < size_z; ++i) {
syundo0730 12:6cd135bf03bd 61 int size_y = pose_size[i];
syundo0730 12:6cd135bf03bd 62 motions[i] = new uint16_t*[size_y];
syundo0730 12:6cd135bf03bd 63 for (int j = 0; j < size_y; ++j) {
syundo0730 12:6cd135bf03bd 64 motions[i][j] = p + size_x * j;
syundo0730 12:6cd135bf03bd 65 }
syundo0730 12:6cd135bf03bd 66 p += size_x * size_y;
syundo0730 12:6cd135bf03bd 67 }
syundo0730 12:6cd135bf03bd 68 }
syundo0730 12:6cd135bf03bd 69
syundo0730 20:abb7852df747 70 bool Controlor::checkid(int id)
syundo0730 13:711f74b2fa33 71 {
syundo0730 13:711f74b2fa33 72 if (id >= 0 && id < motion_size) {
syundo0730 13:711f74b2fa33 73 return true;
syundo0730 13:711f74b2fa33 74 } else {
syundo0730 13:711f74b2fa33 75 return false;
syundo0730 13:711f74b2fa33 76 }
syundo0730 13:711f74b2fa33 77 }
syundo0730 13:711f74b2fa33 78
syundo0730 20:abb7852df747 79 void Controlor::setmotion(const int id)
syundo0730 14:522bb06f0f0d 80 {
syundo0730 20:abb7852df747 81 // TODO : Make OfflineMotion class array and attach by each id. Newing every time is not good!
syundo0730 20:abb7852df747 82 //if (!motion.playing) {
syundo0730 20:abb7852df747 83 //motion = new OfflineMotion(motions[id], pose_size[id], servo_size, pwm, &playing);
syundo0730 20:abb7852df747 84 //tick.attach(motion, &Motion::step, TIMESTEP);
syundo0730 20:abb7852df747 85 attached = true;
syundo0730 20:abb7852df747 86 offline = new OfflineMotion(motions[id], pose_size[id], servo_size, pwm, &playing);
syundo0730 20:abb7852df747 87 tick.attach(offline, &OfflineMotion::step, TIMESTEP);
syundo0730 21:a54bcab078ed 88 //online = new OnlineMotion(1.0, TIMESTEP, servo_size, pwm, &playing);
syundo0730 19:c2ec475367aa 89 //tick.attach(online, &OnlineMotion::step, TIMESTEP);
syundo0730 20:abb7852df747 90 //}
syundo0730 15:e37a8c413e51 91 }
syundo0730 14:522bb06f0f0d 92
syundo0730 20:abb7852df747 93 void Controlor::control()
syundo0730 12:6cd135bf03bd 94 {
syundo0730 20:abb7852df747 95 if (!playing) {
syundo0730 20:abb7852df747 96 if (attached) {
syundo0730 20:abb7852df747 97 tick.detach();
syundo0730 20:abb7852df747 98 delete offline;
syundo0730 20:abb7852df747 99 attached = false;
syundo0730 20:abb7852df747 100 } else {
syundo0730 20:abb7852df747 101 setmotion(1);
syundo0730 20:abb7852df747 102 }
syundo0730 20:abb7852df747 103 }
syundo0730 20:abb7852df747 104
syundo0730 20:abb7852df747 105 //setmotion(1);
syundo0730 17:60de3bfdc70b 106 /*char head = comu->getheader();
syundo0730 16:e65c192b7ecf 107 if (head == 'A') {
syundo0730 16:e65c192b7ecf 108 int id = comu->getid();
syundo0730 16:e65c192b7ecf 109 if (checkid(id)) {
syundo0730 16:e65c192b7ecf 110 setmotion(id);
syundo0730 16:e65c192b7ecf 111 }
syundo0730 16:e65c192b7ecf 112 } else if (head == 'B') {
syundo0730 16:e65c192b7ecf 113 int id = comu->getid();
syundo0730 16:e65c192b7ecf 114 uint16_t val = comu->getservoval();
syundo0730 16:e65c192b7ecf 115 pwm->SetDuty(id, (uint32_t)val);
syundo0730 17:60de3bfdc70b 116 }*/
syundo0730 13:711f74b2fa33 117
syundo0730 12:6cd135bf03bd 118 }