calib_strain_gauges.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import os
4 import rospy
5 import numpy
6 import csv
7 from diagnostic_msgs.msg import DiagnosticArray
8 
9 
10 class Calib_gauges():
11  def __init__(self):
12 
13  self.initial_weight = rospy.get_param('~initial_weight')
14  self.incremental_weight = rospy.get_param('~incremental_weight')
15  self.final_weight = rospy.get_param('~final_weight')
16  self.motor_id = 0
19  self.measurement = []
20  self.headline_0 = []
21  self.temp_0 = []
22  self.headline_1 = []
23  self.temp_1 = []
24 
25  def data_callback(self, data):
26 
27  # iterate over the DiagnosticStatus message
28  for i, value in enumerate(data.status):
29 
30  fields = {}
31 
32  # iterate over values array and store them in a dictionary with index and relative value
33  for key, s in enumerate(value.values):
34 
35  fields[i, s.key] = s.value
36 
37  # iterate over dictionary elements to search the wanted motor_id and the relative strain_gauge value
38  for index, motor_id in fields.items():
39 
40  if fields.get((index[0], 'Motor ID')) == "%s" % self.motor_id:
41  if self.strain_gauges_id == 0:
42  # store measurement in array
43  self.measurement.append(fields.get((index[0], 'Strain Gauge Left')))
44  else:
45  # store measurement in array
46  self.measurement.append(fields.get((index[0], 'Strain Gauge Right')))
47 
48  def run_test(self):
49 
50  self.motor_id = raw_input("insert motor id: ")
51 
52  for self.strain_gauges_id in range(0, 2):
53 
54  self.initial_weight = 0
55 
56  for weight in range(0, self.number_of_tests):
57 
59 
60  print("Apply %s g to strain_gauges %s" % (self.initial_weight, self.strain_gauges_id))
61  raw_input("Press enter when you are done...")
62 
63  # Subscribe diagnostic topic to collect data
64  self.sub = rospy.Subscriber("/diagnostics_agg", DiagnosticArray, self.data_callback)
65  print("Measuring...")
66 
67  rospy.sleep(5.0) # give time to collect measurements
68  self.sub.unregister() # stop subscribing topic
69 
70  self.update_csv_file(self.measurement, self.strain_gauges_id) # write measurements into csv file
71  self.measurement = [] # empty measurement array
72 
73  def update_csv_file(self, measurement, strain_gauges_id):
74 
75  if strain_gauges_id == 0:
76  # create csv to write
77  csv_output = csv.writer(open("Motor_%s_strain_gauge_%s.csv" % (self.motor_id, strain_gauges_id), "wb"),
78  lineterminator='\n')
79  self.headline_0.append(self.initial_weight)
80  csv_output.writerow(self.headline_0) # write headline row with weight values
81  measurement = map(int, measurement) # cast values to int
82  self.temp_0.append(round(numpy.average(measurement))) # compute values average
83  csv_output.writerow(self.temp_0) # write measurements
84 
85  elif strain_gauges_id == 1:
86  # create csv to write
87  csv_output = csv.writer(open("Motor_%s_strain_gauge_%s.csv" % (self.motor_id, strain_gauges_id), "wb"),
88  lineterminator='\n')
89  self.headline_1.append(self.initial_weight)
90  csv_output.writerow(self.headline_1) # write headline row with weight values
91  measurement = map(int, measurement)
92  self.temp_1.append(round(numpy.average(measurement)))
93  csv_output.writerow(self.temp_1) # write measurements
94 
95 
96 if __name__ == '__main__':
97 
98  rospy.init_node("Calib_strain_gauges", anonymous=True)
99  calib = Calib_gauges()
100  calib.run_test()
101 
102 # Output of the file Motor_11_strain_gauge_0.csv
103 # 100,200,300,400,500
104 # 0.0,-1.0,1.0,0.0,1.0
def update_csv_file(self, measurement, strain_gauges_id)


strain_gauge_calibration
Author(s):
autogenerated on Tue Oct 13 2020 03:50:48