Project

General

Profile

Statistics
| Branch: | Revision:

scoutos / scout / usb_serial / src / usbserial.cpp @ 14241f84

History | View | Annotate | Download (3.78 KB)

1
/**
2
 * Copyright (c) 2011 Colony Project
3
 * 
4
 * Permission is hereby granted, free of charge, to any person
5
 * obtaining a copy of this software and associated documentation
6
 * files (the "Software"), to deal in the Software without
7
 * restriction, including without limitation the rights to use,
8
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following
11
 * conditions:
12
 * 
13
 * The above copyright notice and this permission notice shall be
14
 * included in all copies or substantial portions of the Software.
15
 * 
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
 * OTHER DEALINGS IN THE SOFTWARE.
24
 **/
25

    
26

    
27
/**
28
 * @file usbserial.cpp
29
 * @brief USBSerial
30
 *
31
 * Implementation of functions for USB/Serial use.
32
 *
33
 * @author Colony Project, CMU Robotics Club
34
 * @author Lalitha Ganesan
35
 **/
36

    
37
#include "ros/ros.h"
38
#include "usbserial.h"
39
#include <cstdlib>
40

    
41
/**
42
 * @defgroup usbserial USBSerial
43
 * @brief Functions for using the USB/Serial Ports
44
 *
45
 **/
46

    
47
/* USB/Serial state variables */
48
int data; /**< The USB serial data received. */
49
int num_data_bits; /**< The number of bits that store data. */
50
int num_stop_bits; /**< The number of bits that indicate the end of data. */
51
int parity_type; /**< The parity type of the data being sent over. */
52
int baud_rate; /**< The baud rate of the USB/Serial (bits/sec). */
53

    
54

    
55
/*!
56
 * \brief USBSerial driver. This is a ROS node that controls USB/Serial 
57
  * port data.
58
 *
59
 * This is the main function for the usbserial node. It is run when the node
60
 * starts and initializes USB/Serial ports. It then subscribes to the
61
 * send_serial_data topic, and publishes to the receive_serial_data message.
62
 * 
63
 * \param argc The number of command line arguments (should be 1)
64
 * \param argv The array of command line arguments
65
 **/
66
int main(int argc, char **argv){
67
  /* Initialize in ROS the usbserial driver node */
68
  ros::init(argc, argv, "usbserial_driver");
69
  
70
  /* Subscribe to the send_serial_data topic */
71
  ros::Subscriber sub0 = n.subscribe("send_serial_data", send_serial_data);
72

    
73
  /* Publish to the receive_serial_data topic */
74
  ros::Publisher pub0 = n.publish("receive_serial_data", receive_serial_data);
75
  
76
  /* Initialize hardware for USB/Serial ports */
77
  // Hardware init functions here
78

    
79
  ROS_INFO("Ready to set USB/Serial.");
80
  ros::spin();
81

    
82
  return 0;
83
}
84

    
85
/*!
86
 * \brief Sends USB/Serial data.
87
 *
88
 * Sets the state vars based on subscription to the send_serial_data topic.
89
 *
90
 * \param msg The message from the send_serial_data topic, containing info about 
91
 * how data are being sent.
92
 */
93
void send_serial_data(const usbserial::send_serial_data::ConstPtr& msg){
94
  
95
  data = msg->data;
96
  num_data_bits = msg->num_data_bits;
97
  num_stop_bits = msg->num_stop_bits;
98
  parity_type = msg->parity_type;
99
  baud_rate = msg->baud_rate;
100
}
101

    
102
/*!
103
 * \brief Handles received USB/Serial data.
104
 *
105
 * Serves the service receive_serial_data by responding to service requests with the
106
 * usb/serial data.
107
 * \param req The request. Should be empty.
108
 * \param res The response. The fields will be filled with values.
109
 */
110
bool receive_serial_data(usbserial::receive_serial_data::Request &req,
111
    usbserial::receive_serial_data::Response &res){
112
  
113
  res.data = data;
114
  res.num_data_bits = num_data_bits;
115
  res.num_stop_bits = num_stop_bits;
116
  res.parity_type = parity_type;
117
  res.baud_rate = baud_rate;
118
  
119
  return true;
120
}
121