Controlor for Humanoid. Walking trajectory generator, sensor reflection etc.
Dependencies: Adafruit-PWM-Servo-Driver MPU6050 RS300 mbed
Motions.cpp@18:7077bedc37eb, 2013-04-03 (annotated)
- Committer:
- syundo0730
- Date:
- Wed Apr 03 04:56:13 2013 +0000
- Revision:
- 18:7077bedc37eb
- Parent:
- 17:60de3bfdc70b
- Child:
- 19:c2ec475367aa
Real time module. Is step timing suspicious?
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 <string> |
syundo0730 | 17:60de3bfdc70b | 3 | |
syundo0730 | 12:6cd135bf03bd | 4 | #include "Motions.h" |
syundo0730 | 12:6cd135bf03bd | 5 | |
syundo0730 | 17:60de3bfdc70b | 6 | Ticker tick; |
syundo0730 | 12:6cd135bf03bd | 7 | |
syundo0730 | 17:60de3bfdc70b | 8 | //detach may be better to controled from Motions class |
syundo0730 | 13:711f74b2fa33 | 9 | |
syundo0730 | 18:7077bedc37eb | 10 | Motions::Motions(uint16_t* data) : playing(false) |
syundo0730 | 12:6cd135bf03bd | 11 | { |
syundo0730 | 16:e65c192b7ecf | 12 | pwm = new PWM(); |
syundo0730 | 17:60de3bfdc70b | 13 | //comu = new SCI(p28, p27); |
syundo0730 | 17:60de3bfdc70b | 14 | |
syundo0730 | 17:60de3bfdc70b | 15 | comu = new SCI(USBTX, USBRX); |
syundo0730 | 17:60de3bfdc70b | 16 | |
syundo0730 | 12:6cd135bf03bd | 17 | LocalFileSystem* local = new LocalFileSystem("local"); |
syundo0730 | 17:60de3bfdc70b | 18 | |
syundo0730 | 12:6cd135bf03bd | 19 | read("/local/motion.csv", data); |
syundo0730 | 12:6cd135bf03bd | 20 | set(data); |
syundo0730 | 17:60de3bfdc70b | 21 | |
syundo0730 | 17:60de3bfdc70b | 22 | // Motion 0 is Home position |
syundo0730 | 17:60de3bfdc70b | 23 | setmotion(0); |
syundo0730 | 12:6cd135bf03bd | 24 | } |
syundo0730 | 12:6cd135bf03bd | 25 | |
syundo0730 | 12:6cd135bf03bd | 26 | Motions::~Motions() |
syundo0730 | 12:6cd135bf03bd | 27 | { |
syundo0730 | 12:6cd135bf03bd | 28 | for (int i = 0; i < motion_size; i++) { |
syundo0730 | 12:6cd135bf03bd | 29 | delete[] motions[i]; |
syundo0730 | 12:6cd135bf03bd | 30 | } |
syundo0730 | 12:6cd135bf03bd | 31 | delete[] motions; |
syundo0730 | 16:e65c192b7ecf | 32 | |
syundo0730 | 16:e65c192b7ecf | 33 | delete comu; |
syundo0730 | 16:e65c192b7ecf | 34 | delete pwm; |
syundo0730 | 12:6cd135bf03bd | 35 | } |
syundo0730 | 12:6cd135bf03bd | 36 | |
syundo0730 | 12:6cd135bf03bd | 37 | void Motions::read(const string& filename, uint16_t* data) |
syundo0730 | 12:6cd135bf03bd | 38 | { |
syundo0730 | 12:6cd135bf03bd | 39 | CSV csv; |
syundo0730 | 13:711f74b2fa33 | 40 | pose_size = new int; |
syundo0730 | 13:711f74b2fa33 | 41 | csv.read(filename, data, &servo_size, &motion_size, pose_size); |
syundo0730 | 12:6cd135bf03bd | 42 | } |
syundo0730 | 12:6cd135bf03bd | 43 | |
syundo0730 | 12:6cd135bf03bd | 44 | void Motions::set(uint16_t* data) |
syundo0730 | 12:6cd135bf03bd | 45 | { |
syundo0730 | 12:6cd135bf03bd | 46 | int size_z, size_x; |
syundo0730 | 12:6cd135bf03bd | 47 | size_z = motion_size; |
syundo0730 | 12:6cd135bf03bd | 48 | size_x = servo_size; |
syundo0730 | 12:6cd135bf03bd | 49 | |
syundo0730 | 13:711f74b2fa33 | 50 | motions = new uint16_t**[size_z]; |
syundo0730 | 12:6cd135bf03bd | 51 | uint16_t* p = data; |
syundo0730 | 12:6cd135bf03bd | 52 | |
syundo0730 | 12:6cd135bf03bd | 53 | for (int i = 0; i < size_z; ++i) { |
syundo0730 | 12:6cd135bf03bd | 54 | int size_y = pose_size[i]; |
syundo0730 | 12:6cd135bf03bd | 55 | motions[i] = new uint16_t*[size_y]; |
syundo0730 | 12:6cd135bf03bd | 56 | for (int j = 0; j < size_y; ++j) { |
syundo0730 | 12:6cd135bf03bd | 57 | motions[i][j] = p + size_x * j; |
syundo0730 | 12:6cd135bf03bd | 58 | } |
syundo0730 | 12:6cd135bf03bd | 59 | p += size_x * size_y; |
syundo0730 | 12:6cd135bf03bd | 60 | } |
syundo0730 | 12:6cd135bf03bd | 61 | } |
syundo0730 | 12:6cd135bf03bd | 62 | |
syundo0730 | 13:711f74b2fa33 | 63 | bool Motions::checkid(int id) |
syundo0730 | 13:711f74b2fa33 | 64 | { |
syundo0730 | 13:711f74b2fa33 | 65 | if (id >= 0 && id < motion_size) { |
syundo0730 | 13:711f74b2fa33 | 66 | return true; |
syundo0730 | 13:711f74b2fa33 | 67 | } else { |
syundo0730 | 13:711f74b2fa33 | 68 | return false; |
syundo0730 | 13:711f74b2fa33 | 69 | } |
syundo0730 | 13:711f74b2fa33 | 70 | } |
syundo0730 | 13:711f74b2fa33 | 71 | |
syundo0730 | 15:e37a8c413e51 | 72 | void Motions::setmotion(const int id) |
syundo0730 | 14:522bb06f0f0d | 73 | { |
syundo0730 | 17:60de3bfdc70b | 74 | if (!playing) { |
syundo0730 | 18:7077bedc37eb | 75 | //offline = new Motion(motions[id], pose_size[id], servo_size, pwm, &playing); |
syundo0730 | 18:7077bedc37eb | 76 | //tick.attach(offline, &Motion::step, TIMESTEP); |
syundo0730 | 18:7077bedc37eb | 77 | online = new OnlineMotion(1.0, TIMESTEP, servo_size, pwm, &playing); |
syundo0730 | 18:7077bedc37eb | 78 | tick.attach(online, &OnlineMotion::step, TIMESTEP); |
syundo0730 | 15:e37a8c413e51 | 79 | } |
syundo0730 | 15:e37a8c413e51 | 80 | } |
syundo0730 | 14:522bb06f0f0d | 81 | |
syundo0730 | 12:6cd135bf03bd | 82 | void Motions::control() |
syundo0730 | 12:6cd135bf03bd | 83 | { |
syundo0730 | 17:60de3bfdc70b | 84 | setmotion(1); |
syundo0730 | 17:60de3bfdc70b | 85 | /*char head = comu->getheader(); |
syundo0730 | 16:e65c192b7ecf | 86 | if (head == 'A') { |
syundo0730 | 16:e65c192b7ecf | 87 | int id = comu->getid(); |
syundo0730 | 16:e65c192b7ecf | 88 | if (checkid(id)) { |
syundo0730 | 16:e65c192b7ecf | 89 | setmotion(id); |
syundo0730 | 16:e65c192b7ecf | 90 | } |
syundo0730 | 16:e65c192b7ecf | 91 | } else if (head == 'B') { |
syundo0730 | 16:e65c192b7ecf | 92 | int id = comu->getid(); |
syundo0730 | 16:e65c192b7ecf | 93 | uint16_t val = comu->getservoval(); |
syundo0730 | 16:e65c192b7ecf | 94 | pwm->SetDuty(id, (uint32_t)val); |
syundo0730 | 17:60de3bfdc70b | 95 | }*/ |
syundo0730 | 13:711f74b2fa33 | 96 | |
syundo0730 | 12:6cd135bf03bd | 97 | } |