source: flair-src/trunk/lib/FlairSensorActuator/src/AfroBldc_impl.cpp@ 24

Last change on this file since 24 was 15, checked in by Bayard Gildas, 9 years ago

sources reformatted with flair-format-dir script

File size: 1.8 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: 2015/06/15
6// filename: AfroBldc_impl.cpp
7//
8// author: Guillaume Sanahuja
9// Copyright Heudiasyc UMR UTC/CNRS 7253
10//
11// version: $Id: $
12//
13// purpose: objet integrant les moteurs i2c
14//
15//
16/*********************************************************************/
17#include "AfroBldc_impl.h"
18#include "AfroBldc.h"
19#include "Bldc_impl.h"
20#include "I2cPort.h"
21#include <Layout.h>
22#include <SpinBox.h>
23#include <Label.h>
24#include <cvmatrix.h>
25#include <string.h>
26
27#define BASE_ADDRESS 0x29
28#define MAX_VALUE 2047
29
30using std::string;
31using namespace flair::core;
32using namespace flair::gui;
33using namespace flair::sensor;
34using namespace flair::actuator;
35
36AfroBldc_impl::AfroBldc_impl(AfroBldc *self, Layout *layout, I2cPort *i2cport) {
37 this->self = self;
38 this->i2cport = i2cport;
39 nb_mot = self->MotorsCount();
40}
41
42AfroBldc_impl::~AfroBldc_impl() {}
43
44void AfroBldc_impl::SetMotors(float *value) {
45 uint16_t tosend_value[nb_mot];
46
47 for (int i = 0; i < nb_mot; i++)
48 tosend_value[i] = (uint16_t)(MAX_VALUE * value[i]);
49
50 i2cport->GetMutex();
51
52 for (int i = 0; i < nb_mot; i++) {
53 i2cport->SetSlave(BASE_ADDRESS + i);
54 WriteValue(tosend_value[i]);
55 }
56 i2cport->ReleaseMutex();
57}
58
59// I2cPort mutex must be taken before calling this function
60void AfroBldc_impl::WriteValue(uint16_t value) {
61 uint8_t tx[2];
62 ssize_t written;
63
64 tx[0] = (uint8_t)(value >> 3); // msb
65 tx[1] = (value & 0x07); // lsb
66 written = i2cport->Write(tx, 2);
67 if (written < 0) {
68 self->Err("rt_dev_write error (%s)\n", strerror(-written));
69 } else if (written != sizeof(tx)) {
70 self->Err("rt_dev_write error %i/%i\n", written, sizeof(tx));
71 }
72}
Note: See TracBrowser for help on using the repository browser.