Laelaps  2.3.5
RoadNarrows Robotics Small Outdoor Mobile Robot Project
laeMotor.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: Laelasp
4 //
5 // Library: liblaelaps
6 //
7 // File: laeMotor.cxx
8 //
9 /*! \file
10  *
11  * $LastChangedDate: 2015-10-02 15:17:27 -0600 (Fri, 02 Oct 2015) $
12  * $Rev: 4130 $
13  *
14  * \brief Laelaps motors, encoder, and controllers hardware abstraction
15  * support functions.
16  *
17  * \author Robin Knight (robin.knight@roadnarrows.com)
18  *
19  * \par Copyright
20  * \h_copy 2015-2017. RoadNarrows LLC.\n
21  * http://www.roadnarrows.com\n
22  * All Rights Reserved
23  */
24 /*
25  * @EulaBegin@
26  *
27  * Unless otherwise stated explicitly, all materials contained are copyrighted
28  * and may not be used without RoadNarrows LLC's written consent,
29  * except as provided in these terms and conditions or in the copyright
30  * notice (documents and software) or other proprietary notice provided with
31  * the relevant materials.
32  *
33  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY
34  * MEMBERS/EMPLOYEES/CONTRACTORS OF ROADNARROWS OR DISTRIBUTORS OF THIS SOFTWARE
35  * BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR
36  * CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
37  * DOCUMENTATION, EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN
38  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39  *
40  * THE AUTHORS AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
41  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
42  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
43  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
44  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
45  *
46  * @EulaEnd@
47  */
48 ////////////////////////////////////////////////////////////////////////////////
49 
50 #include <sys/types.h>
51 #include <unistd.h>
52 
53 #include <string>
54 
55 #include "rnr/rnrconfig.h"
56 #include "rnr/gpio.h"
57 #include "rnr/log.h"
58 
59 #include "Laelaps/RoboClaw.h"
60 
61 #include "Laelaps/laelaps.h"
62 #include "Laelaps/laeMotor.h"
63 
64 using namespace std;
65 using namespace laelaps;
66 using namespace motor::roboclaw;
67 
68 // -----------------------------------------------------------------------------
69 // LaeMotorCtlrChipSelect Class
70 // -----------------------------------------------------------------------------
71 
72 LaeMotorCtlrChipSelect::LaeMotorCtlrChipSelect()
73 {
74  m_pinGpio = -1;
75  m_fdGpio = -1;
76 }
77 
78 LaeMotorCtlrChipSelect::~LaeMotorCtlrChipSelect()
79 {
80  close();
81 }
82 
83 int LaeMotorCtlrChipSelect::open(int pinGpio)
84 {
85  if( (m_fdGpio = gpioOpen(pinGpio)) < 0 )
86  {
87  LOGERROR("Failed to open motor controllers chip select on GPIO pin %d.",
88  pinGpio);
89 
90  return -LAE_ECODE_NO_RSRC;
91  }
92 
93  m_pinGpio = pinGpio;
94 
95  return LAE_OK;
96 }
97 
98 int LaeMotorCtlrChipSelect::close()
99 {
100  int rc;
101 
102  if( m_fdGpio >= 0 )
103  {
104  rc = gpioClose(m_fdGpio);
105  }
106 
107  m_pinGpio = -1;
108  m_fdGpio = -1;
109 
110  return rc == OK? LAE_OK: -LAE_ECODE_IO;
111 }
112 
113 void LaeMotorCtlrChipSelect::select(int fd, byte_t addrSel)
114 {
115  int value;
116 
117  if( addrSel != m_addrLast )
118  {
119  value = addrSel == LaeMotorCtlrCsHigh? 1: 0;
120  if( gpioQuickWrite(m_fdGpio, value) < 0 )
121  {
122  LOGWARN("Failed to select motor controller 0x%02x.", addrSel);
123  }
124  usleep(100);
125 
126  RoboClawChipSelect::select(fd, addrSel);
127  }
128 }
RoboClaw motor controller class interface.
The <b><i>Laelaps</i></b> namespace encapsulates all <b><i>Laelaps</i></b> related constructs...
Definition: laeAlarms.h:64
Laelaps motors, encoder, and controllers hardware abstraction interfaces.
Top-level package include file.