MiRo-Training
The aim of the project is to develop a software architecture for interacting with Miro using vocal and gestural commands.
 All Classes Files Functions Variables
imu_data_map.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 ################################################################
4 
5 ## \file imu_data_map.py
6 ## \brief The node imu_data_map.py subscribes to the smartwatch's accelerometer data and publish the linear and angular velocities conveniently mapped.
7 ## @n More in details:
8 ## @n It subscribes to the topic /inertial
9 ## @n It reads from that topic the values of linear accelerations along x and y
10 ## @n It maps these accelerations into linear and angular velocities
11 ## @n It publish on /imu_mapping the mapped values
12 ## @n Two modality are available: BASE and ADVANCED
13 ## and they can be selected from launch file
14 
15 
16 
17 import rospy
18 from std_msgs.msg import String
19 from sensor_msgs.msg import Image,CompressedImage,Range,Imu
20 from geometry_msgs.msg import Twist,Pose
21 
22 
23 import math
24 import numpy
25 import time
26 import sys
27 from miro_constants import miro
28 
29 from datetime import datetime
30 
31 ## \brief The Class SmartwatchData allows the subscription to the smartwatch's accelerometer data and publishing of the
32 ## linear and angular velocities conveniently mapped.
33 
34 
36 
37  ##Constructor
38  def __init__(self):
39 
40  ##Last acelleration data received from smartwatch
41  self.last_acc = [0,0,0]
42  ##Control mode set from launch file
43  self.mode = rospy.get_param('control_mode', 'advanced')
44  ##Subscriber to the topic /inertial a message of type Imu
45  self.sub_smartwatch = rospy.Subscriber('/inertial',Imu,self.callback_smartwatch_data,queue_size=1)
46  ##Publisher on the topic /imu_mapping a message of type Twist
47  self.pub_mapping = rospy.Publisher('/imu_mapping', Twist, queue_size=0)
48 
49  ## Callback function that when the data from the smartwatch are received maps them into linear and angular velocities.
50  ## Based on the value of the attribute mode, two different modalities of control are available
51  ## @n <b>mode: basic</b>
52  ## <div style="margin-left:40px;">
53  ## It allows the user to exert three different types of control.
54  ## They correspond to three gesture and therefore to specific ranges of linear acceleration values.
55  ## @n The control gesture are:
56  ## <ul><li><i>Stay Still</i></li>
57  ## <li><i>Turn Right/Left</i></li>
58  ## <li><i>Go Forward/Backward</i></li></ul></div>
59  ## @n <b>mode: advanced</b>
60  ## <div style="margin-left:40px;">
61  ## It allows the user to exert the basic types of control but also combination of them.
62  ## @n All the linear acceleration values beetween the basic ranges are mapped as combination of linear and angular velocities.
63  ## @n<i> e.g The control gesture combination could be Go Forward and Turn Right.</i> </div>
64  def callback_smartwatch_data(self,imu_data):
65 
66  q=Twist()
67  self.last_acc[0] = imu_data.linear_acceleration.x
68  self.last_acc[1] = imu_data.linear_acceleration.y
69  self.last_acc[2] = imu_data.linear_acceleration.z
70 
71  sw_vel=Twist()
72  s_vel=Twist()
73 
74  #Basic Control Gesture: Stay Still
75  if -5 < self.last_acc[0] < 5 and -5 < self.last_acc[1] < 5:
76  sw_vel.linear.x=0.0
77  sw_vel.angular.z=0.0
78  s_vel.linear.x=0.0
79  s_vel.angular.z=0.0
80  print ('miro stay still')
81 
82 
83  #Basic Control Gesture: Go Forward/Backward
84  elif -8 < self.last_acc[0] > 8:
85 
86  sw_vel.linear.x = self.last_acc[0]*50
87  sw_vel.angular.z = 0.0
88  s_vel.linear.x = self.last_acc[0]*50
89  s_vel.angular.z = 0.0
90 
91  #Basic Control Gesture: Turn Right/Left
92  elif -7 < self.last_acc[1] > 7:
93 
94  sw_vel.linear.x = 0.0
95  sw_vel.angular.z = self.last_acc[1]*0.100
96  s_vel.linear.x = 0.0
97  s_vel.angular.z = self.last_acc[1]*0.100
98 
99  #Advanced Control Gesture: Combination of Basic Control Gesture
100  else:
101 
102  sw_vel.linear.x=self.last_acc[0]*50
103  sw_vel.angular.z=self.last_acc[1] #*0.50
104  s_vel.linear.x = 0.0
105  s_vel.angular.z = 0.0
106  #print ('miro move')
107 
108  if self.mode == 'advanced':
109 
110  q = sw_vel
111 
112  if self.mode == 'basic':
113 
114  q = s_vel
115 
116  self.pub_mapping.publish(q)
117 
118  def main (self):
119  rospy.spin()
120 
121 if __name__=='__main__':
122  rospy.init_node('imu_data_map')
123  smartwatch = SmartwatchData()
124  smartwatch.main()
mode
Control mode set from launch file.
Definition: imu_data_map.py:43
The Class SmartwatchData allows the subscription to the smartwatch's accelerometer data and publishin...
Definition: imu_data_map.py:35
def __init__
Constructor.
Definition: imu_data_map.py:38
sub_smartwatch
Subscriber to the topic /inertial a message of type Imu.
Definition: imu_data_map.py:45
pub_mapping
Publisher on the topic /imu_mapping a message of type Twist.
Definition: imu_data_map.py:47
last_acc
Last acelleration data received from smartwatch.
Definition: imu_data_map.py:41
def callback_smartwatch_data
Callback function that when the data from the smartwatch are received maps them into linear and angul...
Definition: imu_data_map.py:64