[3] | 1 | // %flair:license{
|
---|
[15] | 2 | // This file is part of the Flair framework distributed under the
|
---|
| 3 | // CECILL-C License, Version 1.0.
|
---|
[3] | 4 | // %flair:license}
|
---|
| 5 | // created: 2013/11/14
|
---|
| 6 | // filename: Bldc_impl.cpp
|
---|
| 7 | //
|
---|
| 8 | // author: Guillaume Sanahuja
|
---|
| 9 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
| 10 | //
|
---|
| 11 | // version: $Id: $
|
---|
| 12 | //
|
---|
| 13 | // purpose: Virtual class for brushless drivers
|
---|
| 14 | //
|
---|
| 15 | //
|
---|
| 16 | /*********************************************************************/
|
---|
| 17 | #include "Bldc_impl.h"
|
---|
| 18 | #include "Bldc.h"
|
---|
| 19 | #include <GridLayout.h>
|
---|
| 20 | #include <GroupBox.h>
|
---|
| 21 | #include <DoubleSpinBox.h>
|
---|
[214] | 22 | #include <Matrix.h>
|
---|
[3] | 23 | #include <Label.h>
|
---|
| 24 | #include <PushButton.h>
|
---|
| 25 | #include <DataPlot1D.h>
|
---|
| 26 | #include <TabWidget.h>
|
---|
| 27 | #include <Tab.h>
|
---|
| 28 | #include <sstream>
|
---|
| 29 |
|
---|
| 30 | using std::string;
|
---|
| 31 | using std::ostringstream;
|
---|
| 32 | using namespace flair::core;
|
---|
| 33 | using namespace flair::gui;
|
---|
| 34 | using namespace flair::actuator;
|
---|
| 35 |
|
---|
[15] | 36 | Bldc_impl::Bldc_impl(Bldc *self, Layout *layout, string name,
|
---|
| 37 | uint8_t motors_count) {
|
---|
| 38 | this->self = self;
|
---|
| 39 | this->motors_count = motors_count;
|
---|
| 40 | this->layout = layout;
|
---|
| 41 | are_enabled = false;
|
---|
| 42 | is_running = false;
|
---|
| 43 | tested_motor = -1;
|
---|
[3] | 44 |
|
---|
[15] | 45 | values = (float *)malloc(motors_count * sizeof(float));
|
---|
| 46 | power = (float *)malloc(motors_count * sizeof(float));
|
---|
| 47 | for (int i = 0; i < motors_count; i++)
|
---|
| 48 | power[i] = 1;
|
---|
[3] | 49 |
|
---|
[15] | 50 | // station sol
|
---|
| 51 | GroupBox *groupbox = new GroupBox(layout->NewRow(), "bldc");
|
---|
| 52 | flight_time = new Label(groupbox->NewRow(), "flight time");
|
---|
| 53 | min_value = new DoubleSpinBox(groupbox->NewRow(), "min value:", 0, 1, .1, 2);
|
---|
| 54 | max_value =
|
---|
| 55 | new DoubleSpinBox(groupbox->LastRowLastCol(), "max value:", 0, 1, .1, 2);
|
---|
| 56 | test_value =
|
---|
| 57 | new DoubleSpinBox(groupbox->LastRowLastCol(), "test value:", 0, 1, 0.1);
|
---|
[3] | 58 |
|
---|
[15] | 59 | int index = 0;
|
---|
| 60 | button_test = (PushButton **)malloc(motors_count * sizeof(PushButton *));
|
---|
| 61 | for (int i = 0; i < motors_count / 2; i++) {
|
---|
| 62 | for (int j = 0; j < 2; j++) {
|
---|
| 63 | ostringstream test_name;
|
---|
| 64 | test_name << "test motor " << index;
|
---|
| 65 | button_test[index] =
|
---|
| 66 | new PushButton(groupbox->At(2 + i, j), test_name.str());
|
---|
| 67 | index++;
|
---|
[3] | 68 | }
|
---|
[15] | 69 | }
|
---|
[3] | 70 |
|
---|
[15] | 71 | // flight time
|
---|
| 72 | FILE *file;
|
---|
| 73 | file = fopen("/etc/flight_time", "r");
|
---|
| 74 | if (file == NULL) {
|
---|
| 75 | time_sec = 0;
|
---|
| 76 | } else {
|
---|
| 77 | char ligne[32];
|
---|
| 78 | fgets(ligne, 32, file);
|
---|
| 79 | time_sec = atoi(ligne);
|
---|
| 80 | fclose(file);
|
---|
| 81 | }
|
---|
| 82 | flight_time->SetText("total flight time: %is = %imin = %ih\n", time_sec,
|
---|
| 83 | time_sec / 60, time_sec / 3600);
|
---|
[3] | 84 | }
|
---|
| 85 |
|
---|
[15] | 86 | Bldc_impl::Bldc_impl(Bldc *self, uint8_t motors_count) {
|
---|
| 87 | this->self = self;
|
---|
| 88 | this->motors_count = motors_count;
|
---|
| 89 | values = NULL;
|
---|
| 90 | button_test = NULL;
|
---|
| 91 | power = NULL;
|
---|
[3] | 92 | }
|
---|
| 93 |
|
---|
| 94 | Bldc_impl::~Bldc_impl() {
|
---|
[15] | 95 | if (values != NULL)
|
---|
| 96 | free(values);
|
---|
| 97 | if (button_test != NULL)
|
---|
| 98 | free(button_test);
|
---|
| 99 | if (power != NULL)
|
---|
| 100 | free(power);
|
---|
[3] | 101 | }
|
---|
| 102 |
|
---|
[15] | 103 | void Bldc_impl::UseDefaultPlot(TabWidget *tab) {
|
---|
| 104 | Tab *plot_tab = new Tab(tab, "speeds");
|
---|
| 105 | plots = new DataPlot1D(plot_tab->NewRow(), "values", 0, 1);
|
---|
| 106 | for (int i = 0; i < motors_count; i++) {
|
---|
| 107 | plots->AddCurve(self->output->Element(i));
|
---|
| 108 | }
|
---|
[3] | 109 | }
|
---|
| 110 |
|
---|
| 111 | void Bldc_impl::UpdateFrom(const io_data *data) {
|
---|
[214] | 112 | Matrix *input = (Matrix *)data;
|
---|
[15] | 113 | bool is_motor_running = false;
|
---|
[3] | 114 |
|
---|
[15] | 115 | if (values == NULL)
|
---|
| 116 | return; // nothing to do in simulator
|
---|
[3] | 117 |
|
---|
[15] | 118 | if (input->Rows() != motors_count) {
|
---|
| 119 | self->Err("nb motors mismatch\n");
|
---|
| 120 | return;
|
---|
| 121 | }
|
---|
[3] | 122 |
|
---|
[15] | 123 | input->GetMutex();
|
---|
| 124 | for (int i = 0; i < motors_count; i++) {
|
---|
| 125 | if (are_enabled) {
|
---|
| 126 | // Printf("%i %f %f\n",i,input->ValueNoMutex(i,0),power[i]);
|
---|
| 127 | values[i] = power[i] * Sat(input->ValueNoMutex(i, 0));
|
---|
| 128 | // Printf("%i %f\n",i,values[i]);
|
---|
| 129 | } else {
|
---|
| 130 | values[i] = 0;
|
---|
[3] | 131 | }
|
---|
[254] | 132 | if(values[i] != 0) is_motor_running = true;
|
---|
[15] | 133 | }
|
---|
| 134 | input->ReleaseMutex();
|
---|
[3] | 135 |
|
---|
[15] | 136 | if (are_enabled && is_motor_running && !is_running) {
|
---|
| 137 | flight_start_time = GetTime();
|
---|
| 138 | is_running = true;
|
---|
| 139 | }
|
---|
| 140 | if ((!are_enabled || !is_motor_running) && is_running) {
|
---|
| 141 | Time now = GetTime();
|
---|
| 142 | int t_sec;
|
---|
| 143 | FILE *file;
|
---|
| 144 | char ligne[32];
|
---|
[3] | 145 |
|
---|
[15] | 146 | t_sec = (now - flight_start_time) / 1000000000;
|
---|
| 147 | time_sec += t_sec;
|
---|
[3] | 148 |
|
---|
[15] | 149 | Printf("temps de vol: %is = %imin\n", t_sec, t_sec / 60);
|
---|
| 150 | // Printf("temps de vol total: %is = %imin =
|
---|
| 151 | // %ih\n",time_sec,time_sec/60,time_sec/3600);
|
---|
| 152 | flight_time->SetText("total flight time: %is = %imin = %ih\n", time_sec,
|
---|
| 153 | time_sec / 60, time_sec / 3600);
|
---|
[3] | 154 |
|
---|
[15] | 155 | file = fopen("/etc/flight_time", "w");
|
---|
| 156 | if (file == NULL) {
|
---|
| 157 | Printf("Erreur a l'ouverture du fichier d'info vol\n");
|
---|
| 158 | } else {
|
---|
| 159 | sprintf(ligne, "%i", time_sec);
|
---|
| 160 | fputs(ligne, file);
|
---|
| 161 | fclose(file);
|
---|
[3] | 162 | }
|
---|
[15] | 163 | is_running = false;
|
---|
| 164 | }
|
---|
[3] | 165 |
|
---|
[15] | 166 | for (int i = 0; i < motors_count; i++) {
|
---|
| 167 | if (button_test[i]->Clicked() == true) {
|
---|
| 168 | if (!are_enabled) {
|
---|
| 169 | tested_motor = i;
|
---|
| 170 | test_start_time = GetTime();
|
---|
| 171 | LockUserInterface();
|
---|
| 172 | } else {
|
---|
| 173 | self->Warn("testing motor is not possible when enabled\n");
|
---|
| 174 | }
|
---|
[3] | 175 | }
|
---|
[15] | 176 | }
|
---|
| 177 | if (tested_motor != -1) {
|
---|
| 178 | for (int i = 0; i < motors_count; i++) {
|
---|
| 179 | values[i] = 0;
|
---|
| 180 | }
|
---|
| 181 | values[tested_motor] = test_value->Value();
|
---|
[3] | 182 |
|
---|
[15] | 183 | if (GetTime() > (test_start_time + 2 * 1000000000)) {
|
---|
| 184 | tested_motor = -1;
|
---|
| 185 | UnlockUserInterface();
|
---|
[3] | 186 | }
|
---|
[15] | 187 | }
|
---|
[3] | 188 |
|
---|
[15] | 189 | self->SetMotors(values);
|
---|
| 190 | self->output->SetDataTime(data->DataTime());
|
---|
| 191 | self->ProcessUpdate(self->output);
|
---|
[3] | 192 | }
|
---|
| 193 |
|
---|
| 194 | float Bldc_impl::Sat(float value) {
|
---|
[15] | 195 | float result = value;
|
---|
[3] | 196 |
|
---|
[15] | 197 | if (result < min_value->Value()) {
|
---|
| 198 | result = min_value->Value();
|
---|
| 199 | }
|
---|
| 200 | if (result > max_value->Value()) {
|
---|
| 201 | result = max_value->Value();
|
---|
| 202 | }
|
---|
[3] | 203 |
|
---|
[15] | 204 | return result;
|
---|
[3] | 205 | }
|
---|
| 206 |
|
---|
[15] | 207 | void Bldc_impl::LockUserInterface(void) const { layout->setEnabled(false); }
|
---|
[3] | 208 |
|
---|
[15] | 209 | void Bldc_impl::UnlockUserInterface(void) const { layout->setEnabled(true); }
|
---|