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

Last change on this file since 382 was 340, checked in by Sanahuja Guillaume, 5 years ago

add servos

File size: 5.3 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() {
87 if (values != NULL)
88 free(values);
89 if (button_test != NULL)
90 free(button_test);
91 if (power != NULL)
92 free(power);
93}
94
95void Bldc_impl::UseDefaultPlot(TabWidget *tab) {
96 Tab *plot_tab = new Tab(tab, "speeds");
97 plots = new DataPlot1D(plot_tab->NewRow(), "values", 0, 1);
98 for (int i = 0; i < motors_count; i++) {
99 plots->AddCurve(self->output->Element(i));
100 }
101}
102
103void Bldc_impl::UpdateFrom(const io_data *data) {
104 Matrix *input = (Matrix *)data;
105 bool is_motor_running = false;
106
107 if (values == NULL)
108 return; // nothing to do in simulator
109
110 if (input->Rows() != motors_count) {
111 self->Err("nb motors mismatch\n");
112 return;
113 }
114
115 input->GetMutex();
116 for (int i = 0; i < motors_count; i++) {
117 if (are_enabled) {
118 //Printf("%i %f %f\n",i,input->ValueNoMutex(i,0),power[i]);
119 values[i] = power[i] * Sat(input->ValueNoMutex(i, 0));
120 // Printf("%i %f\n",i,values[i]);
121 } else {
122 values[i] = 0;
123 }
124 if(values[i] != 0) is_motor_running = true;
125 }
126 input->ReleaseMutex();
127
128 if (are_enabled && is_motor_running && !is_running) {
129 flight_start_time = GetTime();
130 is_running = true;
131 }
132 if ((!are_enabled || !is_motor_running) && is_running) {
133 Time now = GetTime();
134 int t_sec;
135 FILE *file;
136 char ligne[32];
137
138 t_sec = (now - flight_start_time) / 1000000000;
139 time_sec += t_sec;
140
141 Printf("temps de vol: %is = %imin\n", t_sec, t_sec / 60);
142 // Printf("temps de vol total: %is = %imin =
143 // %ih\n",time_sec,time_sec/60,time_sec/3600);
144 flight_time->SetText("total flight time: %is = %imin = %ih\n", time_sec,
145 time_sec / 60, time_sec / 3600);
146
147 file = fopen("/etc/flight_time", "w");
148 if (file == NULL) {
149 Printf("Erreur a l'ouverture du fichier d'info vol\n");
150 } else {
151 sprintf(ligne, "%i", time_sec);
152 fputs(ligne, file);
153 fclose(file);
154 }
155 is_running = false;
156 }
157
158 for (int i = 0; i < motors_count; i++) {
159 if (button_test[i]->Clicked() == true) {
160 if (!are_enabled) {
161 tested_motor = i;
162 test_start_time = GetTime();
163 LockUserInterface();
164 } else {
165 self->Warn("testing motor is not possible when enabled\n");
166 }
167 }
168 }
169 if (tested_motor != -1) {
170 for (int i = 0; i < motors_count; i++) {
171 values[i] = 0;
172 }
173 values[tested_motor] = test_value->Value();
174
175 if (GetTime() > (test_start_time + 2 * 1000000000)) {
176 tested_motor = -1;
177 UnlockUserInterface();
178 }
179 }
180
181 self->SetMotors(values);
182 self->output->SetDataTime(data->DataTime());
183 self->ProcessUpdate(self->output);
184}
185
186float Bldc_impl::Sat(float value) {
187 float result = value;
188
189 if (result < min_value->Value()) {
190 result = min_value->Value();
191 }
192 if (result > max_value->Value()) {
193 result = max_value->Value();
194 }
195
196 return result;
197}
198
199void Bldc_impl::LockUserInterface(void) const { layout->setEnabled(false); }
200
201void Bldc_impl::UnlockUserInterface(void) const { layout->setEnabled(true); }
Note: See TracBrowser for help on using the repository browser.