vesc_interface.h
Go to the documentation of this file.
1 // Copyright 2020 F1TENTH Foundation
2 //
3 // Redistribution and use in source and binary forms, with or without modification, are permitted
4 // provided that the following conditions are met:
5 //
6 // 1. Redistributions of source code must retain the above copyright notice, this list of conditions
7 // and the following disclaimer.
8 //
9 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
10 // of conditions and the following disclaimer in the documentation and/or other materials
11 // provided with the distribution.
12 //
13 // 3. Neither the name of the copyright holder nor the names of its contributors may be used
14 // to endorse or promote products derived from this software without specific prior
15 // written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
18 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
19 // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
24 // WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 // -*- mode:c++; fill-column: 100; -*-
27 
28 #ifndef VESC_DRIVER_VESC_INTERFACE_H_
29 #define VESC_DRIVER_VESC_INTERFACE_H_
30 
31 #include <exception>
32 #include <functional>
33 #include <memory>
34 #include <sstream>
35 #include <stdexcept>
36 #include <string>
37 
39 
40 namespace vesc_driver
41 {
42 
47 {
48 public:
49  typedef std::function<void (const VescPacketConstPtr&)> PacketHandlerFunction;
50  typedef std::function<void (const std::string&)> ErrorHandlerFunction;
51 
63  VescInterface(const std::string& port = std::string(),
64  const PacketHandlerFunction& packet_handler = PacketHandlerFunction(),
65  const ErrorHandlerFunction& error_handler = ErrorHandlerFunction());
66 
70  VescInterface(const VescInterface &) = delete;
71  VescInterface & operator=(const VescInterface &) = delete;
72 
77 
81  void setPacketHandler(const PacketHandlerFunction& handler);
82 
87  void setErrorHandler(const ErrorHandlerFunction& handler);
88 
94  void connect(const std::string& port);
95 
99  void disconnect();
100 
106  bool isConnected() const;
107 
111  void send(const VescPacket& packet);
112 
113  void requestFWVersion();
114  void requestState();
115  void setDutyCycle(double duty_cycle);
116  void setCurrent(double current);
117  void setBrake(double brake);
118  void setSpeed(double speed);
119  void setPosition(double position);
120  void setServo(double servo);
121 
122 private:
123  // Pimpl - hide serial port members from class users
124  class Impl;
125  std::unique_ptr<Impl> impl_;
126 };
127 
128 // todo: review
129 class SerialException : public std::exception
130 {
131  // Disable copy constructors
133  std::string e_what_;
134 public:
135  explicit SerialException(const char *description)
136  {
137  std::stringstream ss;
138  ss << "SerialException " << description << " failed.";
139  e_what_ = ss.str();
140  }
141  SerialException(const SerialException& other) : e_what_(other.e_what_) {}
142  virtual ~SerialException() throw() {}
143  virtual const char* what() const throw()
144  {
145  return e_what_.c_str();
146  }
147 };
148 
149 } // namespace vesc_driver
150 
151 #endif // VESC_DRIVER_VESC_INTERFACE_H_
VescInterface(const std::string &port=std::string(), const PacketHandlerFunction &packet_handler=PacketHandlerFunction(), const ErrorHandlerFunction &error_handler=ErrorHandlerFunction())
std::function< void(const VescPacketConstPtr &)> PacketHandlerFunction
void setPosition(double position)
std::unique_ptr< Impl > impl_
std::function< void(const std::string &)> ErrorHandlerFunction
void setDutyCycle(double duty_cycle)
void connect(const std::string &port)
void setCurrent(double current)
virtual const char * what() const
void setBrake(double brake)
SerialException(const char *description)
void setServo(double servo)
void send(const VescPacket &packet)
void setSpeed(double speed)
SerialException(const SerialException &other)
void setErrorHandler(const ErrorHandlerFunction &handler)
VescInterface & operator=(const VescInterface &)=delete
void setPacketHandler(const PacketHandlerFunction &handler)


vesc_driver
Author(s): Michael T. Boulet , Joshua Whitley
autogenerated on Sun Apr 18 2021 02:48:01