![](/media/cache/profiles/e5815151957be36ad2085b7a1a02c5cc.jpg.50x50_q85.jpg)
Controlor for Humanoid. Walking trajectory generator, sensor reflection etc.
Dependencies: Adafruit-PWM-Servo-Driver MPU6050 RS300 mbed
Motions.cpp@19:c2ec475367aa, 2013-04-07 (annotated)
- Committer:
- syundo0730
- Date:
- Sun Apr 07 06:59:33 2013 +0000
- Revision:
- 19:c2ec475367aa
- Parent:
- 18:7077bedc37eb
New Motion read method (not good) was added.
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 | 19:c2ec475367aa | 42 | //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 | 43 | } |
syundo0730 | 12:6cd135bf03bd | 44 | |
syundo0730 | 12:6cd135bf03bd | 45 | void Motions::set(uint16_t* data) |
syundo0730 | 12:6cd135bf03bd | 46 | { |
syundo0730 | 12:6cd135bf03bd | 47 | int size_z, size_x; |
syundo0730 | 12:6cd135bf03bd | 48 | size_z = motion_size; |
syundo0730 | 12:6cd135bf03bd | 49 | size_x = servo_size; |
syundo0730 | 12:6cd135bf03bd | 50 | |
syundo0730 | 13:711f74b2fa33 | 51 | motions = new uint16_t**[size_z]; |
syundo0730 | 12:6cd135bf03bd | 52 | uint16_t* p = data; |
syundo0730 | 12:6cd135bf03bd | 53 | |
syundo0730 | 12:6cd135bf03bd | 54 | for (int i = 0; i < size_z; ++i) { |
syundo0730 | 12:6cd135bf03bd | 55 | int size_y = pose_size[i]; |
syundo0730 | 12:6cd135bf03bd | 56 | motions[i] = new uint16_t*[size_y]; |
syundo0730 | 12:6cd135bf03bd | 57 | for (int j = 0; j < size_y; ++j) { |
syundo0730 | 12:6cd135bf03bd | 58 | motions[i][j] = p + size_x * j; |
syundo0730 | 12:6cd135bf03bd | 59 | } |
syundo0730 | 12:6cd135bf03bd | 60 | p += size_x * size_y; |
syundo0730 | 12:6cd135bf03bd | 61 | } |
syundo0730 | 12:6cd135bf03bd | 62 | } |
syundo0730 | 12:6cd135bf03bd | 63 | |
syundo0730 | 13:711f74b2fa33 | 64 | bool Motions::checkid(int id) |
syundo0730 | 13:711f74b2fa33 | 65 | { |
syundo0730 | 13:711f74b2fa33 | 66 | if (id >= 0 && id < motion_size) { |
syundo0730 | 13:711f74b2fa33 | 67 | return true; |
syundo0730 | 13:711f74b2fa33 | 68 | } else { |
syundo0730 | 13:711f74b2fa33 | 69 | return false; |
syundo0730 | 13:711f74b2fa33 | 70 | } |
syundo0730 | 13:711f74b2fa33 | 71 | } |
syundo0730 | 13:711f74b2fa33 | 72 | |
syundo0730 | 15:e37a8c413e51 | 73 | void Motions::setmotion(const int id) |
syundo0730 | 14:522bb06f0f0d | 74 | { |
syundo0730 | 17:60de3bfdc70b | 75 | if (!playing) { |
syundo0730 | 19:c2ec475367aa | 76 | offline = new Motion(motions[id], pose_size[id], servo_size, pwm, &playing); |
syundo0730 | 19:c2ec475367aa | 77 | tick.attach(offline, &Motion::step, TIMESTEP); |
syundo0730 | 19:c2ec475367aa | 78 | //online = new OnlineMotion(3.0, TIMESTEP, servo_size, pwm, &playing); |
syundo0730 | 19:c2ec475367aa | 79 | //tick.attach(online, &OnlineMotion::step, TIMESTEP); |
syundo0730 | 15:e37a8c413e51 | 80 | } |
syundo0730 | 15:e37a8c413e51 | 81 | } |
syundo0730 | 14:522bb06f0f0d | 82 | |
syundo0730 | 12:6cd135bf03bd | 83 | void Motions::control() |
syundo0730 | 12:6cd135bf03bd | 84 | { |
syundo0730 | 17:60de3bfdc70b | 85 | setmotion(1); |
syundo0730 | 17:60de3bfdc70b | 86 | /*char head = comu->getheader(); |
syundo0730 | 16:e65c192b7ecf | 87 | if (head == 'A') { |
syundo0730 | 16:e65c192b7ecf | 88 | int id = comu->getid(); |
syundo0730 | 16:e65c192b7ecf | 89 | if (checkid(id)) { |
syundo0730 | 16:e65c192b7ecf | 90 | setmotion(id); |
syundo0730 | 16:e65c192b7ecf | 91 | } |
syundo0730 | 16:e65c192b7ecf | 92 | } else if (head == 'B') { |
syundo0730 | 16:e65c192b7ecf | 93 | int id = comu->getid(); |
syundo0730 | 16:e65c192b7ecf | 94 | uint16_t val = comu->getservoval(); |
syundo0730 | 16:e65c192b7ecf | 95 | pwm->SetDuty(id, (uint32_t)val); |
syundo0730 | 17:60de3bfdc70b | 96 | }*/ |
syundo0730 | 13:711f74b2fa33 | 97 | |
syundo0730 | 12:6cd135bf03bd | 98 | } |