Project

General

Profile

Statistics
| Branch: | Revision:

scoutos / scout / usb_serial / src / usbserial.cpp @ c492be62

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
 * @file usbserial.cpp
28
 * @brief USBSerial
29
 *
30
 * Implementation of functions for USB/Serial use.
31
 *
32
 * @author Colony Project, CMU Robotics Club
33
 * @author Lalitha Ganesan
34
 **/
35

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

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

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

    
53

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

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

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

    
81
  return 0;
82
}
83

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

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