3
3
import rospy
4
4
from brass_gazebo_config_manager .srv import *
5
5
from brass_gazebo_battery .srv import *
6
+ from std_msgs .msg import Float64
7
+ import os
8
+ import json
6
9
7
10
8
11
class CP1_Instructions (object ):
9
12
ros_node = '/battery_monitor_client'
10
13
model_name = '/battery_demo_model'
14
+ config_file = os .path .expanduser ("~/cp1/plugins_config.json" )
15
+ sleep_interval = 5
11
16
12
17
def __init__ (self ):
13
18
self .set_configuration_srv = rospy .ServiceProxy (self .ros_node + self .model_name + '/set_robot_configuration' , SetConfig )
14
19
self .set_charging_srv = rospy .ServiceProxy (self .ros_node + self .model_name + '/set_charging' , SetCharging )
15
20
21
+ with open (self .config_file ) as db :
22
+ data = json .load (db )
23
+ self .battery_capacity = data ['battery_capacity' ]
24
+ self .charging_rate = data ['charging_rate' ]
25
+ self .battery_charge = self .battery_capacity
26
+
16
27
def set_config (self , config_id ):
17
28
print ("Setting configuration to " + config_id )
18
29
res = self .set_configuration_srv (config_id )
@@ -24,8 +35,8 @@ def set_config(self, config_id):
24
35
def set_charging (self , charging ):
25
36
return self .set_charging_srv (charging )
26
37
27
- def dock (self , seconds ):
28
- rospy .loginfo ("The bot is docked and start charging for {0} seconds" . format ( seconds ) )
38
+ def dock (self ):
39
+ rospy .loginfo ("The bot is docked and start charging" )
29
40
self .set_charging (1 )
30
41
return True
31
42
@@ -34,8 +45,23 @@ def undock(self):
34
45
rospy .loginfo ("The bot is now undocked" )
35
46
return True
36
47
37
- def charge (self , seconds ):
38
- self .dock (seconds )
39
- rospy .sleep (rospy .Duration .from_sec (seconds ))
48
+ def charge (self ):
49
+ self .dock ()
50
+ while not self .is_fully_charged ():
51
+ rospy .sleep (rospy .Duration .from_sec (self .sleep_interval ))
40
52
self .undock ()
41
53
return True , "Charging is done"
54
+
55
+ def is_fully_charged (self ):
56
+ if self .battery_charge == self .battery_capacity :
57
+ rospy .loginfo ("Battery is fully charged." )
58
+ return True
59
+ else :
60
+ return False
61
+
62
+ def get_charge (self , msg ):
63
+ self .battery_charge = msg .data
64
+
65
+ def track_battery_charge (self ):
66
+ """starts monitoring battery and update battery_charge"""
67
+ rospy .Subscriber ("/mobile_base/commands/charge_level_mwh" , Float64 , self .get_charge )
0 commit comments