-
Notifications
You must be signed in to change notification settings - Fork 0
/
uno_writer.cpp
125 lines (107 loc) · 2.92 KB
/
uno_writer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "ros/ros.h"
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <unistd.h>
#include <cstdlib>
#include <termios.h>
#include <fcntl.h>
#include "std_msgs/String.h"
#include <sstream>
#include <iomanip>
int open_port(void);
// Declare file descriptor for the port.
int fd;
int len = 10;
std::string send ("");
float angRef = 1.80;
float torqueRef = 0.30;
int wsuc = 0;
int main(int argc, char **argv)
{
ros::init(argc, argv, "UNO_reader");
ros::NodeHandle n;
ros::Publisher uno = n.advertise<std_msgs::String> ("uno", 1000);
// Under 40 Hz : system has much latency --> SET >= 40
ros::Rate loop_rate(40);
try
{
// Open serial port with defined parameters
ROS_INFO("Opening serial connection.");
open_port();
//std::cout << fd << std::endl;
ros::Duration(2).sleep();
// Enter the main loop
while (ros::ok())
{
// -------WRITING SEQUENCE ----START--------
std::stringstream stream;
stream << std::fixed << std::setprecision(2) << torqueRef << "," << angRef << "a";
send = stream.str();
wsuc = write(fd, send.c_str(), len);
//std::cout << send << std::endl;
// --------WRITING SEQUENCE -------END-------
ros::spinOnce();
loop_rate.sleep();
}
}
catch (std::exception)
{
ROS_ERROR("Failed to open the port");
}
close( fd );
}
// ------------PORT OPENING FUNCTION -------------------------
int open_port()
{
// Open port
fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
// ERROR: Could not open port!
std::cout << "open_port: Unable to open /dev/ttyACM0" << std::endl;
}
else
{
fcntl(fd, F_SETFL, FNDELAY);
// Port opened successfully!
std::cout << "In Open port fd = " << fd << std::endl;
}
// Read the configureation of the port.
struct termios options_2;
tcgetattr( fd, &options_2 );
// Set I/O speed of the port.
cfsetispeed( &options_2, B115200 );
cfsetospeed( &options_2, B115200 );
// Enable the receiver and set local mode.
options_2.c_cflag |= ( CLOCAL | CREAD );
// Set the new options for the port.
tcsetattr(fd, TCSAFLUSH, &options_2);
// Set the Charactor size: Mask the character size bits
options_2.c_cflag &= ~CSIZE;
// Select 8 data bits
options_2.c_cflag |= CS8;
// Set parity - No Parity (8N1)
options_2.c_cflag &= ~PARENB;
options_2.c_cflag &= ~CSTOPB;
options_2.c_cflag &= ~CSIZE;
options_2.c_cflag |= CS8;
// Disable Software Flow control
options_2.c_iflag &= ~(IXON | IXOFF | IXANY);
// Chose raw (not processed) output
options_2.c_oflag &= ~OPOST;
if ( tcsetattr( fd, TCSANOW, &options_2 ) == -1 ){
// ERROR: set tcsetattr not successfull!
std::cout << "Error with setting attributions." << std::endl;
}
else{
// tcsetattr OK!
std::cout << "Attributes settings succeeded" << std::endl;
}
usleep(1500);
tcflush(fd,TCIOFLUSH); // Flush I/O queue.
return(fd);
}
// ----PORT OPENING FUNCTION -------------END------------