head_motion.cpp
Go to the documentation of this file.
1  /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted
8  provided that the following conditions are met:
9 
10  *Redistributions of source code must retain the above copyright notice, this list of
11  conditions and the following disclaimer.
12  *Redistributions in binary form must reproduce the above copyright notice, this list of
13  conditions and the following disclaimer in the documentation and/or other materials provided
14  with the distribution.
15  *Neither the name of the University of Aveiro nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  ***************************************************************************************************/
27 
48 #include <ros/ros.h>
49 
50 #include <hitec5980sg/hitec5980sg.h>
51 #include <geometry_msgs/Point.h>
52 
53 using namespace ros;
54 using namespace std;
55 
56 short unsigned int x_before = 1950;
57 short unsigned int y_before = 1470;
58 
59 
60 class Head
61 {
62  public:
63 
64  Head(std::string device):
65  servos(device.c_str())
66  {
67 
68  }
69 
70  void positionHandler(const geometry_msgs::Point& point)
71  {
72  if ((point.z <= 0) && (point.y <= 0))
73  {
74  servos.SetPosition(53,x_before);
75  //std::cout << "x_before: " << x_before << "y_before" << y_before << std::endl;
76  //servos.SetSpeedPosition(53,0);
77  servos.SetPosition(41,y_before);
78  //servos.SetSpeedPosition(41,0);
79  }
80  else
81  {
82  if ((point.z >= 0) && (point.z <= 255) && (point.y >= 0) && (point.y <= 255))//validate velocity
83  {
84  if (point.x == 1)
85  {
86  servos.SetPosition(53,1600);
87  x_before = servos.SetSpeedPosition(53,point.y);
88 
89  servos.SetPosition(41,2000);
90  y_before = servos.SetSpeedPosition(41,point.z);
91  }
92  if (point.x == 2)
93  {
94  servos.SetPosition(53,1600);
95  x_before = servos.SetSpeedPosition(53,point.y);
96 
97  servos.SetPosition(41,600);
98  y_before = servos.SetSpeedPosition(41,point.z);
99  }
100  if (point.x == 3)
101  {
102  servos.SetPosition(53,2000);
103  x_before = servos.SetSpeedPosition(53,point.y);
104 
105  servos.SetPosition(41,2000);
106  y_before = servos.SetSpeedPosition(41,point.z);
107  }
108  if (point.x == 4)
109  {
110  servos.SetPosition(53,2000);
111  x_before = servos.SetSpeedPosition(53,point.y);
112 
113  servos.SetPosition(41,600);
114  y_before = servos.SetSpeedPosition(41,point.z);
115  }
116  }
117  else
118  {
119  std::cout << "invalid velocity: " << point.z << std::endl;
120  }
121  }
122  }
123 
124  hitec_5980SG servos;
125 };
126 
127 int main(int argc, char** argv)
128 {
129  ros::init( argc, argv, "head_motion" );//nome
130 
131  ros::NodeHandle n("~");
132 
133  Head head("/dev/ttyUSB0");
134  std::cout << "initializing head motion..." << std::endl;
135 
136  ros::Subscriber sub = n.subscribe("/find_arrow_state", 1,&Head::positionHandler,&head);
137 
138  ros::spin();
139 
140  return 0;
141 }
hitec_5980SG servos
short unsigned int x_before
Definition: head_motion.cpp:56
void positionHandler(const geometry_msgs::Point &point)
Definition: head_motion.cpp:70
short unsigned int y_before
Definition: head_motion.cpp:57
Head(std::string device)
Definition: head_motion.cpp:64
int main(int argc, char **argv)


humanoid_control
Author(s): Emilio Estrelinha, César Sousa
autogenerated on Mon Mar 2 2015 01:31:42