[10] | 1 | // %flair:license{
|
---|
[15] | 2 | // This file is part of the Flair framework distributed under the
|
---|
| 3 | // CECILL-C License, Version 1.0.
|
---|
[10] | 4 | // %flair:license}
|
---|
[7] | 5 | // created: 2011/05/01
|
---|
| 6 | // filename: TrajectoryGenerator1D_impl.cpp
|
---|
| 7 | //
|
---|
| 8 | // author: Guillaume Sanahuja
|
---|
| 9 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
| 10 | //
|
---|
| 11 | // version: $Id: $
|
---|
| 12 | //
|
---|
| 13 | // purpose: Class generating a trajectory in 1D
|
---|
| 14 | //
|
---|
| 15 | //
|
---|
| 16 | /*********************************************************************/
|
---|
| 17 |
|
---|
| 18 | #include "TrajectoryGenerator1D.h"
|
---|
| 19 | #include "TrajectoryGenerator1D_impl.h"
|
---|
[214] | 20 | #include <Matrix.h>
|
---|
[7] | 21 | #include <Layout.h>
|
---|
| 22 | #include <GroupBox.h>
|
---|
| 23 | #include <DoubleSpinBox.h>
|
---|
| 24 | #include <cmath>
|
---|
| 25 |
|
---|
| 26 | using std::string;
|
---|
| 27 | using namespace flair::core;
|
---|
| 28 | using namespace flair::gui;
|
---|
| 29 | using namespace flair::filter;
|
---|
| 30 |
|
---|
[15] | 31 | TrajectoryGenerator1D_impl::TrajectoryGenerator1D_impl(
|
---|
| 32 | TrajectoryGenerator1D *self, const LayoutPosition *position, string name,
|
---|
| 33 | string unit) {
|
---|
| 34 | first_update = true;
|
---|
| 35 | is_started = false;
|
---|
[7] | 36 |
|
---|
[15] | 37 | // init UI
|
---|
| 38 | GroupBox *reglages_groupbox = new GroupBox(position, name);
|
---|
| 39 | T = new DoubleSpinBox(reglages_groupbox->NewRow(), "period, 0 for auto:",
|
---|
| 40 | " s", 0, 1, 0.01);
|
---|
| 41 | if (unit == "") {
|
---|
| 42 | max_veloctity =
|
---|
| 43 | new DoubleSpinBox(reglages_groupbox->LastRowLastCol(),
|
---|
| 44 | "velocity max (absolute):", 0., 200000, 1);
|
---|
| 45 | } else {
|
---|
| 46 | max_veloctity = new DoubleSpinBox(reglages_groupbox->LastRowLastCol(),
|
---|
| 47 | "velocity max (absolute):",
|
---|
| 48 | " " + unit + "/s", 0., 200000, 1);
|
---|
| 49 | }
|
---|
| 50 | if (unit == "") {
|
---|
| 51 | acceleration = new DoubleSpinBox(reglages_groupbox->LastRowLastCol(),
|
---|
| 52 | "acceleration (absolute):", 0., 10, 1, 3);
|
---|
| 53 | } else {
|
---|
| 54 | acceleration = new DoubleSpinBox(reglages_groupbox->LastRowLastCol(),
|
---|
| 55 | "acceleration (absolute):",
|
---|
| 56 | " " + unit + "/s²", 0., 200000, 1);
|
---|
| 57 | }
|
---|
[7] | 58 |
|
---|
[15] | 59 | Reset();
|
---|
[7] | 60 |
|
---|
[15] | 61 | // init matrix
|
---|
[318] | 62 | MatrixDescriptor *desc = new MatrixDescriptor(2, 1);
|
---|
[15] | 63 | desc->SetElementName(0, 0, "pos");
|
---|
| 64 | desc->SetElementName(1, 0, "vel");
|
---|
[214] | 65 | output = new Matrix(self, desc, floatType, name);
|
---|
[148] | 66 | delete desc;
|
---|
[7] | 67 |
|
---|
[15] | 68 | output->SetValue(0, 0, pos);
|
---|
| 69 | output->SetValue(1, 0, v);
|
---|
[7] | 70 | }
|
---|
| 71 |
|
---|
[15] | 72 | TrajectoryGenerator1D_impl::~TrajectoryGenerator1D_impl() {}
|
---|
[7] | 73 |
|
---|
| 74 | void TrajectoryGenerator1D_impl::Reset(void) {
|
---|
[15] | 75 | pos = 0;
|
---|
| 76 | v = 0;
|
---|
| 77 | pos_off = 0;
|
---|
| 78 | vel_off = 0;
|
---|
[7] | 79 | }
|
---|
| 80 |
|
---|
[177] | 81 | void TrajectoryGenerator1D_impl::StartTraj(float start_pos, float end_pos,float startVelocity) {
|
---|
[15] | 82 | is_started = true;
|
---|
| 83 | is_finished = false;
|
---|
| 84 | first_update = true;
|
---|
[7] | 85 |
|
---|
[15] | 86 | // configure trajectory
|
---|
[205] | 87 | this->start_pos = start_pos;
|
---|
| 88 | this->end_pos = end_pos;
|
---|
[15] | 89 | pos = start_pos;
|
---|
| 90 | acc = acceleration->Value();
|
---|
[177] | 91 | v = startVelocity;
|
---|
| 92 | if (fabs(v) > fabs(max_veloctity->Value())) {
|
---|
| 93 | if (v > 0)
|
---|
| 94 | v = max_veloctity->Value();
|
---|
| 95 | else
|
---|
| 96 | v = -max_veloctity->Value();
|
---|
| 97 | }
|
---|
[205] | 98 | if (end_pos < start_pos) {
|
---|
[15] | 99 | acc = -acc;
|
---|
| 100 | // max_veloctity=-max_veloctity;
|
---|
| 101 | }
|
---|
[7] | 102 | }
|
---|
| 103 |
|
---|
[15] | 104 | // revoir l'interet du stop?
|
---|
[7] | 105 | void TrajectoryGenerator1D_impl::StopTraj(void) {
|
---|
[15] | 106 | is_started = false;
|
---|
| 107 | v = 0;
|
---|
| 108 | // output->SetValue(1,0,v);
|
---|
[7] | 109 | }
|
---|
| 110 |
|
---|
[205] | 111 | float TrajectoryGenerator1D_impl::GetPercentageOfCompletion(void) const {
|
---|
| 112 | if(!is_started) {
|
---|
| 113 | return 0;
|
---|
| 114 | } else {
|
---|
| 115 | return (pos-start_pos)/(end_pos-start_pos)*100;
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[7] | 119 | void TrajectoryGenerator1D_impl::Update(Time time) {
|
---|
[15] | 120 | float delta_t;
|
---|
[7] | 121 |
|
---|
[15] | 122 | if (T->Value() == 0) {
|
---|
| 123 | if (first_update == true) {
|
---|
| 124 | first_update = false;
|
---|
| 125 | previous_time = time;
|
---|
| 126 | output->GetMutex();
|
---|
| 127 | output->SetValueNoMutex(0, 0, pos + pos_off);
|
---|
| 128 | output->SetValueNoMutex(1, 0, v + vel_off);
|
---|
| 129 | output->ReleaseMutex();
|
---|
[7] | 130 |
|
---|
[15] | 131 | output->SetDataTime(time);
|
---|
| 132 | return;
|
---|
[7] | 133 | } else {
|
---|
[15] | 134 | delta_t = (float)(time - previous_time) / 1000000000.;
|
---|
[7] | 135 | }
|
---|
[15] | 136 | } else {
|
---|
| 137 | delta_t = T->Value();
|
---|
| 138 | }
|
---|
| 139 | previous_time = time;
|
---|
[7] | 140 |
|
---|
[15] | 141 | if (is_started == true) {
|
---|
| 142 | if (is_finished == false) {
|
---|
| 143 | v += acc * delta_t;
|
---|
| 144 | if (fabs(v) > fabs(max_veloctity->Value())) {
|
---|
| 145 | if (v > 0)
|
---|
| 146 | v = max_veloctity->Value();
|
---|
| 147 | else
|
---|
| 148 | v = -max_veloctity->Value();
|
---|
| 149 | }
|
---|
| 150 | pos += v * delta_t;
|
---|
[205] | 151 | if (end_pos - v * v / (2 * acc) <= pos && v >= 0)
|
---|
[15] | 152 | acc = -acc;
|
---|
[205] | 153 | if (end_pos - v * v / (2 * acc) >= pos && v < 0)
|
---|
[15] | 154 | acc = -acc;
|
---|
[205] | 155 | if (pos >= end_pos && v >= 0)
|
---|
[15] | 156 | is_finished = true;
|
---|
[205] | 157 | if (pos <= end_pos && v < 0)
|
---|
[15] | 158 | is_finished = true;
|
---|
[7] | 159 | }
|
---|
[15] | 160 | // else
|
---|
| 161 | if (is_finished == true) {
|
---|
| 162 | v = 0;
|
---|
[205] | 163 | pos = end_pos;
|
---|
[15] | 164 | }
|
---|
| 165 | }
|
---|
[7] | 166 |
|
---|
[15] | 167 | // on prend une fois pour toute les mutex et on fait des accès directs
|
---|
| 168 | output->GetMutex();
|
---|
| 169 | output->SetValueNoMutex(0, 0, pos + pos_off);
|
---|
| 170 | output->SetValueNoMutex(1, 0, v + vel_off);
|
---|
| 171 | output->ReleaseMutex();
|
---|
[7] | 172 |
|
---|
[15] | 173 | output->SetDataTime(time);
|
---|
[7] | 174 | }
|
---|