source: flair-src/trunk/lib/FlairSensorActuator/src/Bldc_impl.cpp@ 334

Last change on this file since 334 was 254, checked in by Sanahuja Guillaume, 6 years ago

change watchdog timeout with gcs
remove unnecessary messages on bldc

File size: 5.5 KB
Line 
1// %flair:license{
2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
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>
22#include <Matrix.h>
23#include <Label.h>
24#include <PushButton.h>
25#include <DataPlot1D.h>
26#include <TabWidget.h>
27#include <Tab.h>
28#include <sstream>
29
30using std::string;
31using std::ostringstream;
32using namespace flair::core;
33using namespace flair::gui;
34using namespace flair::actuator;
35
36Bldc_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;
44
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;
49
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);
58
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++;
68 }
69 }
70
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);
84}
85
86Bldc_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;
92}
93
94Bldc_impl::~Bldc_impl() {
95 if (values != NULL)
96 free(values);
97 if (button_test != NULL)
98 free(button_test);
99 if (power != NULL)
100 free(power);
101}
102
103void 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 }
109}
110
111void Bldc_impl::UpdateFrom(const io_data *data) {
112 Matrix *input = (Matrix *)data;
113 bool is_motor_running = false;
114
115 if (values == NULL)
116 return; // nothing to do in simulator
117
118 if (input->Rows() != motors_count) {
119 self->Err("nb motors mismatch\n");
120 return;
121 }
122
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;
131 }
132 if(values[i] != 0) is_motor_running = true;
133 }
134 input->ReleaseMutex();
135
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];
145
146 t_sec = (now - flight_start_time) / 1000000000;
147 time_sec += t_sec;
148
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);
154
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);
162 }
163 is_running = false;
164 }
165
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 }
175 }
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();
182
183 if (GetTime() > (test_start_time + 2 * 1000000000)) {
184 tested_motor = -1;
185 UnlockUserInterface();
186 }
187 }
188
189 self->SetMotors(values);
190 self->output->SetDataTime(data->DataTime());
191 self->ProcessUpdate(self->output);
192}
193
194float Bldc_impl::Sat(float value) {
195 float result = value;
196
197 if (result < min_value->Value()) {
198 result = min_value->Value();
199 }
200 if (result > max_value->Value()) {
201 result = max_value->Value();
202 }
203
204 return result;
205}
206
207void Bldc_impl::LockUserInterface(void) const { layout->setEnabled(false); }
208
209void Bldc_impl::UnlockUserInterface(void) const { layout->setEnabled(true); }
Note: See TracBrowser for help on using the repository browser.