MRPT  1.9.9
CServoeNeck.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #pragma once
11 
13 #include <mrpt/core/exceptions.h>
14 
15 namespace mrpt::hwdrivers
16 {
17 /** A USB-interface for a custom "robotic neck" designed at MAPIR lab.
18  * \ingroup mrpt_hwdrivers_grp */
20 {
21  public:
22  CServoeNeck();
23  ~CServoeNeck() override;
24 
25  /** Gets the firmware version of the eNeck board.
26  * \param out_firmwareVersion: [OUTPUT] A string containing the firmware
27  * version.
28  * \return Whether or not the procedure succeded.
29  */
30  bool queryFirmwareVersion(std::string& out_firmwareVersion);
31 
32  /** Gets the current angle of the servo (in radians within (-pi,pi))
33  * \param Angle: [OUT] The current angle.
34  * \param Servo: [IN] The id of the servo (in our ATMEGA16, from 0 to 2).
35  * \return Whether or not the procedure succeded.
36  */
37  bool getCurrentAngle(double& angle, const uint8_t servo = 0);
38 
39  /** Turns the servo up to the specified angle (in radians in the range
40  * -pi,pi, other values will be saturated to the maximum or the mininum)
41  * \param Angle: the desired angle to turn.
42  * \param Servo: the id of the servo to move (in our ATMEGA16, from 0 to
43  * 2).
44  * \param Fast: indicates if the servo must reach the angle at maximum
45  * speed
46  * \return Whether or not the procedure succeded.
47  */
48  bool setAngle(double angle, const uint8_t servo = 0, bool fast = false);
49 
50  /** Turns the servo up to the specified angle (in radians in the range
51  * -pi,pi, other values will be saturated to the maximum or the mininum)
52  * \param Angle: the desired angle to turn.
53  * \param Servo: the id of the servo to move (in our ATMEGA16, from 0 to
54  * 2).
55  * \param Speed: indicates the speed of the servo
56  * \return Whether or not the procedure succeded.
57  */
58  bool setAngleAndSpeed(
59  double angle, const uint8_t servo, const uint8_t speed);
60 
61  /** Turns the servo up to the specified angle (in radians in the range
62  * -pi,pi) filtered by average with the last N specified angles.
63  * \param Angle: the new desired angle to turn.
64  * \param Servo: the id of the servo to move (in our ATMEGA16, from 0 to
65  * 2).
66  * \param Fast: indicates if the servo must reach the angle at maximum
67  * speed
68  * \return Whether or not the procedure succeded.
69  */
70  bool setAngleWithFilter(
71  double angle, const uint8_t servo = 0, bool fast = false);
72 
73  /** Disables the servo so the neck will be loose.
74  * \param Servo: the id of the servo to move (in our ATMEGA16, from 0 to
75  * 2).
76  * \return Whether or not the procedure succeded.
77  */
78  bool disableServo(const uint8_t servo = 0);
79 
80  /** Enables the servo so the neck will be tight.
81  * \param Servo: the id of the servo to move (in our ATMEGA16, from 0 to
82  * 2).
83  * \return Whether or not the procedure succeded.
84  */
85  bool enableServo(const uint8_t servo = 0);
86 
87  /** Centers the servo at zero position
88  */
89  bool center(const uint8_t servo = 0);
90 
91  /** Gets the truncate factor of the turn
92  */
93  double getTruncateFactor() { return m_TruncateFactor; }
94  /** Gets the truncate factor of the turn
95  */
96  void setTruncateFactor(const double factor)
97  {
98  ASSERT_(factor > 0 && factor < 1);
99  m_TruncateFactor = factor;
100  }
101 
102  /** Gets the truncate factor of the turn
103  */
104  void setNumberOfPreviousAngles(const unsigned int number)
105  {
106  m_NumPrevAngles = number;
107  }
108 
109  /** Gets the truncate factor of the turn
110  */
111  unsigned int getNumberOfPreviousAngles() { return m_NumPrevAngles; }
112  /**Load the Offset values for each servo
113  */
114  void setOffsets(float offset0, float offset1, float offset2);
115 
116  protected:
117  /** A copy of the device serial number (to open the USB FTDI chip). */
119  /** The value set in the ICR register within the ATMEGA16 controller. */
120  double m_MaxValue{10000};
121  /** The range of turn of the servo will be truncated to
122  * "+-m_truncate_factor*(pi/2)". */
123  double m_TruncateFactor{0.5};
124  /** A vector containing the last N angles which where passed to the servo
125  * (for averaging) */
126  std::deque<double> m_PrevAngles;
127  /** Number of previous angles to store for averaging */
128  unsigned int m_NumPrevAngles{5};
129  /** The offset used for each servo */
130  std::vector<float> m_offsets;
131 
132  bool setRegisterValue(
133  const uint16_t value, const uint8_t servo = 0, bool fast = false);
135  const uint16_t value, const uint8_t servo, const uint16_t speed);
136  bool getRegisterValue(uint16_t& value, const uint8_t servo = 0);
137 
138  private:
139  /** Converts from a decimal angle (in radians) to the corresponding register
140  * value for the ATMEGA16 controller (for inner use only).
141  * \param The angle to convert.
142  * \return The value of the register to send.
143  */
144  unsigned int angle2RegValue(const double angle); // Angle in rad
145 
146  /** Converts from a certain value of the ATMEGA16 PWM register to the
147  * corresponding decimal angle (for inner use only).
148  * \param The value to convert.
149  * \return The corresponding angle.
150  */
151  double regValue2angle(const uint16_t value);
152 
153  /** Tries to connect to the USB device (if disconnected).
154  * \return True on connection OK, false on error.
155  */
157 
158 }; // End of class
159 
160 } // namespace mrpt::hwdrivers
unsigned int angle2RegValue(const double angle)
Converts from a decimal angle (in radians) to the corresponding register value for the ATMEGA16 contr...
Definition: CServoeNeck.cpp:76
bool setRegisterValueAndSpeed(const uint16_t value, const uint8_t servo, const uint16_t speed)
bool setAngle(double angle, const uint8_t servo=0, bool fast=false)
Turns the servo up to the specified angle (in radians in the range -pi,pi, other values will be satur...
bool checkConnectionAndConnect()
Tries to connect to the USB device (if disconnected).
bool queryFirmwareVersion(std::string &out_firmwareVersion)
Gets the firmware version of the eNeck board.
Definition: CServoeNeck.cpp:45
Contains classes for various device interfaces.
unsigned int m_NumPrevAngles
Number of previous angles to store for averaging.
Definition: CServoeNeck.h:128
void setNumberOfPreviousAngles(const unsigned int number)
Gets the truncate factor of the turn.
Definition: CServoeNeck.h:104
A USB-interface for a custom "robotic neck" designed at MAPIR lab.
Definition: CServoeNeck.h:19
bool center(const uint8_t servo=0)
Centers the servo at zero position.
bool setAngleWithFilter(double angle, const uint8_t servo=0, bool fast=false)
Turns the servo up to the specified angle (in radians in the range -pi,pi) filtered by average with t...
std::vector< float > m_offsets
The offset used for each servo.
Definition: CServoeNeck.h:130
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
bool getCurrentAngle(double &angle, const uint8_t servo=0)
Gets the current angle of the servo (in radians within (-pi,pi))
bool disableServo(const uint8_t servo=0)
Disables the servo so the neck will be loose.
bool enableServo(const uint8_t servo=0)
Enables the servo so the neck will be tight.
GLsizei const GLchar ** string
Definition: glext.h:4116
unsigned int getNumberOfPreviousAngles()
Gets the truncate factor of the turn.
Definition: CServoeNeck.h:111
void setOffsets(float offset0, float offset1, float offset2)
Load the Offset values for each servo.
double getTruncateFactor()
Gets the truncate factor of the turn.
Definition: CServoeNeck.h:93
void setTruncateFactor(const double factor)
Gets the truncate factor of the turn.
Definition: CServoeNeck.h:96
A definition of a CStream actually representing a USB connection to a FTDI chip.
std::string m_usbSerialNumber
A copy of the device serial number (to open the USB FTDI chip).
Definition: CServoeNeck.h:118
bool getRegisterValue(uint16_t &value, const uint8_t servo=0)
double m_TruncateFactor
The range of turn of the servo will be truncated to "+-m_truncate_factor*(pi/2)". ...
Definition: CServoeNeck.h:123
GLsizei const GLfloat * value
Definition: glext.h:4134
double m_MaxValue
The value set in the ICR register within the ATMEGA16 controller.
Definition: CServoeNeck.h:120
bool setAngleAndSpeed(double angle, const uint8_t servo, const uint8_t speed)
Turns the servo up to the specified angle (in radians in the range -pi,pi, other values will be satur...
std::deque< double > m_PrevAngles
A vector containing the last N angles which where passed to the servo (for averaging) ...
Definition: CServoeNeck.h:126
double regValue2angle(const uint16_t value)
Converts from a certain value of the ATMEGA16 PWM register to the corresponding decimal angle (for in...
Definition: CServoeNeck.cpp:98
bool setRegisterValue(const uint16_t value, const uint8_t servo=0, bool fast=false)



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: c1796881b Sat Nov 16 19:04:34 2019 +0100 at sáb nov 16 19:15:10 CET 2019