~jan-winkler-84/libcflie/master

« back to all changes in this revision

Viewing changes to include/cflie/CCrazyRadio.h

  • Committer: Jan Winkler
  • Author(s): Boaz Stolk
  • Date: 2015-03-02 07:33:26 UTC
  • Revision ID: git-v1:229be1e3f23839f669ae98e59fbc037b2adee4cd
fix using namespace std

Conflicts:
        src/cflie/CCrazyflie.cpp

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
#define __C_CRAZY_RADIO_H__
34
34
 
35
35
 
36
 
#include <iostream>
37
36
#include <list>
38
37
#include <string>
39
 
#include <sstream>
40
38
#include <libusb-1.0/libusb.h>
41
 
#include <stdio.h>
42
 
#include <stdlib.h>
43
 
#include <string.h>
44
 
#include <unistd.h>
45
39
 
46
40
#include "CCRTPPacket.h"
47
41
 
48
 
using namespace std;
49
 
 
50
 
 
51
42
/*! \brief Power levels to configure the radio dongle with */
52
43
enum Power {
53
44
  /*! \brief Power at -18dbm */
63
54
 
64
55
/*! \brief Communication class to connect to and communicate via the
65
56
    CrazyRadio USB dongle.
66
 
    
 
57
 
67
58
    The class is capable of finding the CrazyRadio USB dongle on the
68
59
    host computer, open and maintain a connection, and send/receive
69
60
    data when communicating with the Crazyflie Nano copter using the
73
64
  // Variables
74
65
  /*! \brief The radio URI as supplied when initializing the class
75
66
      instance */
76
 
  string m_strRadioIdentifier;
 
67
  std::string m_strRadioIdentifier;
77
68
  /*! \brief The current USB context as supplied by libusb */
78
69
  libusb_context *m_ctxContext;
79
70
  libusb_device *m_devDevice;
80
71
  libusb_device_handle *m_hndlDevice;
81
72
  int m_nARC;
82
73
  int m_nChannel;
83
 
  string m_strDataRate;
 
74
  std::string m_strDataRate;
84
75
  int m_nARDTime;
85
76
  int m_nARDBytes;
86
77
  enum Power m_enumPower;
88
79
  int m_bContCarrier;
89
80
  float m_fDeviceVersion;
90
81
  bool m_bAckReceived;
91
 
  list<CCRTPPacket*> m_lstLoggingPackets;
92
 
  
 
82
  std::list<CCRTPPacket*> m_lstLoggingPackets;
 
83
 
93
84
  // Functions
94
 
  list<libusb_device*> listDevices(int nVendorID, int nProductID);
 
85
  std::list<libusb_device*> listDevices(int nVendorID, int nProductID);
95
86
  bool openUSBDongle();
96
87
  bool claimInterface(int nInterface);
97
88
  void closeDevice();
98
89
 
99
90
  CCRTPPacket *readACK();
100
 
  
 
91
 
101
92
  CCRTPPacket *writeData(void *vdData, int nLength);
102
93
  bool writeControl(void *vdData, int nLength, uint8_t u8Request, uint16_t u16Value, uint16_t u16Index);
103
94
  bool readData(void *vdData, int &nMaxLength);
104
95
 
105
96
  void setARC(int nARC);
106
97
  void setChannel(int nChannel);
107
 
  void setDataRate(string strDataRate);
 
98
  void setDataRate(std::string strDataRate);
108
99
  void setARDBytes(int nARDBytes);
109
100
  void setARDTime(int nARDTime);
110
101
  void setAddress(char *cAddress);
112
103
 
113
104
public:
114
105
  /*! \brief Constructor for the radio communication class
115
 
    
 
106
 
116
107
    \param strRadioIdentifier URI for the radio to be opened,
117
108
    e.g. "radio://<dongle-no>/<channel-no>/<datarate>". */
118
 
  CCrazyRadio(string strRadioIdentifier);
 
109
  CCrazyRadio(std::string strRadioIdentifier);
119
110
  /*! \brief Destructor for the radio communication class */
120
111
  ~CCrazyRadio();
121
 
  
 
112
 
122
113
  /*! \brief Function to start the radio communication
123
 
    
 
114
 
124
115
    The first available USB dongle will be opened and claimed for
125
116
    communication. The connection will be maintained and used to
126
117
    communicate with a Crazyflie Nano quadcopter in range.
129
120
    made and 'false' if no dongle could be found (or any other
130
121
    USB-related error came up - this is not handled here). */
131
122
  bool startRadio();
132
 
  
 
123
 
133
124
  /*! \brief Returns the current setting for power usage by the USB
134
125
      dongle
135
 
    
 
126
 
136
127
    \return Value denoting the current power settings reserved for
137
128
    communication */
138
129
  enum Power power();
139
130
  /*! \brief Set the power level to be used for communication purposes
140
 
    
 
131
 
141
132
    \param enumPower The level of power that is being used for
142
133
    communication. The integer value maps to one of the entries of the
143
134
    Power enum. */
144
135
  void setPower(enum Power enumPower);
145
 
  
 
136
 
146
137
  /*! \brief Sends the given packet's payload to the copter
147
 
    
 
138
 
148
139
    \param crtpSend The packet which supplied header and payload
149
140
    information to send to the copter */
150
141
  CCRTPPacket *sendPacket(CCRTPPacket *crtpSend, bool bDeleteAfterwards = false);
151
 
  
 
142
 
152
143
  /*! \brief Sends the given packet and waits for a reply.
153
 
    
 
144
 
154
145
    Internally, this function calls the more elaborate
155
146
    sendAndReceive() function supplying parameters for retrying and
156
147
    waiting. Convenience function signature.
157
 
    
 
148
 
158
149
    \param crtpSend Packet to send
159
150
    \param bDeleteAfterwards Whether or not the packet to send is
160
151
    deleted internally after sending it
161
 
    
 
152
 
162
153
    \return Packet containing the reply or NULL if no reply was
163
154
    received (after retrying). */
164
155
  CCRTPPacket *sendAndReceive(CCRTPPacket *crtpSend, bool bDeleteAfterwards = false);
165
 
  
 
156
 
166
157
  /*! \brief Sends the given packet and waits for a reply.
167
 
    
 
158
 
168
159
    Sends out the CCRTPPacket instance denoted by crtpSend on the
169
160
    given port and channel. Retries a number of times and waits
170
161
    between each retry whether or not an answer was received (in this
171
162
    case, dummy packets are sent in order to receive replies).
172
 
    
 
163
 
173
164
    \param crtpSend Packet to send
174
 
    
 
165
 
175
166
    \param nPort Port number on which to send this packet (and where
176
167
    to wait for the reply)
177
168
    \param nChannel Channel number on which to send this packet (and
181
172
    \param nRetries Number of retries (re-sending) before giving up on
182
173
    an answer
183
174
    \param nMicrosecondsWait Microseconds to wait between two re-sends
184
 
    
 
175
 
185
176
    \return Packet containing the reply or NULL if no reply was
186
177
    received (after retrying). */
187
178
  CCRTPPacket *sendAndReceive(CCRTPPacket *crtpSend, int nPort, int nChannel, bool bDeleteAfterwards = true, int nRetries = 10, int nMicrosecondsWait = 100);
188
 
  
 
179
 
189
180
  /*! \brief Sends out an empty dummy packet
190
 
    
 
181
 
191
182
    Only contains the payload `0xff`, as used for empty packet
192
183
    requests. Mostly used for waiting or keepalive.
193
 
    
 
184
 
194
185
    \return Boolean value denoting whether sending the dummy packet
195
186
    worked or not. */
196
187
  bool sendDummyPacket();
197
 
  
 
188
 
198
189
  /*! \brief Waits for the next non-empty packet.
199
 
    
 
190
 
200
191
    Sends out dummy packets until a reply is non-empty and then
201
192
    returns this reply.
202
 
    
 
193
 
203
194
    \return Packet contaning a non-empty reply. */
204
195
  CCRTPPacket *waitForPacket();
205
 
  
 
196
 
206
197
  /*! \brief Whether or not the copter is answering sent packets.
207
 
    
 
198
 
208
199
    Returns whether the copter is actually answering sent packets with
209
200
    a set ACK flag. If this is not the case, it is either switched off
210
201
    or out of range.
211
 
    
 
202
 
212
203
    \return Returns true if the copter is returning the ACK flag properly, false otherwise. */
213
204
  bool ackReceived();
214
205
  /*! \brief Whether or not the USB connection is still operational.
215
 
    
 
206
 
216
207
    Checks if the USB read/write calls yielded any errors.
217
 
    
 
208
 
218
209
    \return Returns true if the connection is working properly and
219
210
    false otherwise. */
220
211
  bool usbOK();
221
 
  
 
212
 
222
213
  /*! \brief Extracting all logging related packets
223
 
    
 
214
 
224
215
    Returns a list of all collected logging related (i.e. originating
225
216
    from port 5) packets. This is called by the CCrazyflie class
226
217
    automatically when performing cycle().
227
 
    
 
218
 
228
219
    \return List of CCRTPPacket instances collected from port 5
229
220
    (logging). */
230
 
  list<CCRTPPacket*> popLoggingPackets();
 
221
  std::list<CCRTPPacket*> popLoggingPackets();
231
222
};
232
223
 
233
224