[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/13
|
---|
| 6 | // filename: XBldc_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 Xufo
|
---|
| 14 | //
|
---|
| 15 | //
|
---|
| 16 | /*********************************************************************/
|
---|
| 17 |
|
---|
| 18 | #include "XBldc_impl.h"
|
---|
| 19 | #include "XBldc.h"
|
---|
| 20 | #include <I2cPort.h>
|
---|
| 21 | #include <cvmatrix.h>
|
---|
| 22 | #include <string.h>
|
---|
| 23 |
|
---|
| 24 | #define BASE_ADDRESS 0x01
|
---|
| 25 | #define MAX_VALUE 200
|
---|
| 26 |
|
---|
| 27 | using namespace flair::core;
|
---|
| 28 | using namespace flair::gui;
|
---|
| 29 | using namespace flair::actuator;
|
---|
| 30 |
|
---|
[15] | 31 | XBldc_impl::XBldc_impl(XBldc *self, I2cPort *i2cport) {
|
---|
| 32 | this->self = self;
|
---|
| 33 | this->i2cport = i2cport;
|
---|
[3] | 34 |
|
---|
[15] | 35 | i2cport->SetTxTimeout(5000000);
|
---|
[3] | 36 | }
|
---|
| 37 |
|
---|
[15] | 38 | XBldc_impl::~XBldc_impl() {}
|
---|
[3] | 39 |
|
---|
[15] | 40 | void XBldc_impl::SetMotors(float *value) {
|
---|
| 41 | uint8_t tosend_value[5];
|
---|
[3] | 42 |
|
---|
[15] | 43 | for (int i = 0; i < 4; i++)
|
---|
| 44 | tosend_value[i] = (uint8_t)(MAX_VALUE * value[i]);
|
---|
[3] | 45 |
|
---|
[15] | 46 | ssize_t written;
|
---|
| 47 | tosend_value[4] = 0xaa;
|
---|
| 48 | for (int i = 0; i < 4; i++)
|
---|
| 49 | tosend_value[4] += tosend_value[i];
|
---|
| 50 | self->Err("please verify motors adresses\n");
|
---|
| 51 | // todo faire la procedure changement addresse par la station sol
|
---|
| 52 | /*
|
---|
| 53 | i2cport->GetMutex();
|
---|
| 54 | i2cport->SetSlave(BASE_ADDRESS);
|
---|
[3] | 55 |
|
---|
[15] | 56 | written =i2cport->Write(tosend_value, sizeof(tosend_value));
|
---|
| 57 | if(written<0)
|
---|
| 58 | {
|
---|
| 59 | self->Err("rt_dev_write error (%s)\n",strerror(-written));
|
---|
| 60 | }
|
---|
| 61 | else if (written != sizeof(tosend_value))
|
---|
| 62 | {
|
---|
| 63 | self->Err("rt_dev_write error %i/%i\n",written,sizeof(tosend_value));
|
---|
| 64 | }
|
---|
[3] | 65 |
|
---|
[15] | 66 | i2cport->ReleaseMutex();*/
|
---|
[3] | 67 | }
|
---|
| 68 | /*
|
---|
| 69 | void XBldc_impl::StartTest(uint8_t moteur)
|
---|
| 70 | {
|
---|
| 71 | unsigned char tx[4];
|
---|
| 72 | ssize_t written;
|
---|
| 73 |
|
---|
| 74 | tx[0]=251;
|
---|
| 75 | tx[1]=moteur;
|
---|
| 76 | tx[2]=0;
|
---|
| 77 | tx[3]=231+moteur;
|
---|
| 78 |
|
---|
| 79 | i2cport->GetMutex();
|
---|
| 80 | i2cport->SetSlave(BASE_ADDRESS);
|
---|
| 81 |
|
---|
| 82 | written =i2cport->Write(tx, sizeof(tx));
|
---|
| 83 | if(written<0)
|
---|
| 84 | {
|
---|
| 85 | self->Err("rt_dev_write error (%s)\n",strerror(-written));
|
---|
| 86 | }
|
---|
| 87 | else if (written != sizeof(tx))
|
---|
| 88 | {
|
---|
| 89 | self->Err("rt_dev_write %i/%i error\n",written,sizeof(tx));
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | i2cport->ReleaseMutex();
|
---|
| 93 | }
|
---|
| 94 | */
|
---|
| 95 | void XBldc_impl::ChangeDirection(uint8_t moteur) {
|
---|
[15] | 96 | unsigned char tx[4];
|
---|
| 97 | ssize_t written;
|
---|
[3] | 98 |
|
---|
[15] | 99 | tx[0] = 254;
|
---|
| 100 | tx[1] = moteur;
|
---|
| 101 | tx[2] = 0;
|
---|
| 102 | tx[3] = 234 + moteur;
|
---|
[3] | 103 |
|
---|
[15] | 104 | i2cport->GetMutex();
|
---|
| 105 | i2cport->SetSlave(BASE_ADDRESS);
|
---|
[3] | 106 |
|
---|
[15] | 107 | written = i2cport->Write(tx, sizeof(tx));
|
---|
| 108 | if (written < 0) {
|
---|
| 109 | self->Err("rt_dev_write (%s) error\n", strerror(-written));
|
---|
| 110 | } else if (written != sizeof(tx)) {
|
---|
| 111 | self->Err("rt_dev_write %i/%i error\n", written, sizeof(tx));
|
---|
| 112 | }
|
---|
[3] | 113 |
|
---|
[15] | 114 | i2cport->ReleaseMutex();
|
---|
[3] | 115 | }
|
---|
| 116 |
|
---|
[15] | 117 | void XBldc_impl::ChangeAdress(uint8_t old_add, uint8_t new_add) {
|
---|
| 118 | unsigned char tx[4];
|
---|
| 119 | ssize_t written;
|
---|
[3] | 120 |
|
---|
[15] | 121 | tx[0] = 250;
|
---|
| 122 | tx[1] = old_add;
|
---|
| 123 | tx[2] = new_add;
|
---|
| 124 | tx[3] = 230 + old_add + new_add;
|
---|
[3] | 125 |
|
---|
[15] | 126 | i2cport->GetMutex();
|
---|
| 127 | i2cport->SetSlave(BASE_ADDRESS);
|
---|
[3] | 128 |
|
---|
[15] | 129 | written = i2cport->Write(tx, sizeof(tx));
|
---|
| 130 | if (written < 0) {
|
---|
| 131 | self->Err("rt_dev_write error (%s)\n", strerror(-written));
|
---|
| 132 | } else if (written != sizeof(tx)) {
|
---|
| 133 | self->Err("rt_dev_write error %i/%i\n", written, sizeof(tx));
|
---|
| 134 | }
|
---|
[3] | 135 |
|
---|
[15] | 136 | i2cport->ReleaseMutex();
|
---|
[3] | 137 | }
|
---|
| 138 |
|
---|
[15] | 139 | uint8_t XBldc_impl::Sat(float value, uint8_t min, uint8_t max) {
|
---|
| 140 | uint8_t sat_value = (uint8_t)value;
|
---|
| 141 |
|
---|
| 142 | if (value > ((float)sat_value + 0.5))
|
---|
| 143 | sat_value++;
|
---|
| 144 |
|
---|
| 145 | if (value < (float)min)
|
---|
| 146 | sat_value = min;
|
---|
| 147 | if (value > (float)max)
|
---|
| 148 | sat_value = max;
|
---|
| 149 |
|
---|
| 150 | return sat_value;
|
---|
[3] | 151 | }
|
---|