class queueRead(): def __init__(self): rospy.init_node("queueRead", anonymous=True) self.nodename = rospy.get_name() rospy.loginfo("%s started" % self.nodename) ### initialize variables self.topic_vel = 'cmd_vel' self.topic_stop = 'motor_stop' ### get parameters #### self.SAS_name = (rospy.get_param("~SAS_NAME")) self.SAS_value = (rospy.get_param("~SAS_VALUE")) self.namespace = (rospy.get_param("~TOPIC_NAMESPACE")) self.bot_id = (rospy.get_param("~BOT_ID")) self.bus_service = ServiceBusService( service_namespace=self.namespace, shared_access_key_name=self.SAS_name, shared_access_key_value=self.SAS_value) self.bus_service.create_subscription((self.topic_vel + self.bot_id), self.bot_id) self.bus_service.create_subscription((self.topic_stop + self.bot_id), self.bot_id) ### setup ### self.pub_lmotor = rospy.Publisher('lmotor_cmd', Float32, queue_size=10) self.pub_rmotor = rospy.Publisher('rmotor_cmd', Float32, queue_size=10) self.pub_motor_stop = rospy.Publisher('motorStop', Int16, queue_size=10) def read(self): while not rospy.is_shutdown(): try: msg = self.bus_service.receive_subscription_message( (self.topic_vel + self.bot_id), self.bot_id, peek_lock=False) if msg.body is not None: res = list(map(float, msg.body.split(' '))) self.pub_lmotor.publish(res[0]) self.pub_lmotor.publish(res[1]) msg = self.bus_service.receive_subscription_message( (self.topic_stop + self.bot_id), self.bot_id, peek_lock=False) if msg.body is not None: self.pub_lmotor.publish(int(msg.body)) except: pass
from azure.servicebus.control_client import ServiceBusService, Message, Topic, Rule, DEFAULT_RULE_NAME GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) GPIO.setup(8, GPIO.OUT, initial=GPIO.LOW) bus_service = ServiceBusService( service_namespace='***REPLACE ME, with your Service Bus Namespace Name***', shared_access_key_name= '***REPLACE ME, with your Shared Access Policy Name***', shared_access_key_value= '***REPLACE ME, with your Shared Access Key Value***') while True: msg = bus_service.receive_subscription_message( '***REPLACE ME with your Service Bus Topic Name***', '***REPLACE ME with your Service Bus Topic Subscription Name***', peek_lock=True) if msg.body is not None: value = json.loads(msg.body.decode("utf-8")) print(value["status"]) if value["status"] == 1: GPIO.output(8, GPIO.HIGH) print('The LED has been turned ON.') elif value["status"] == 0: GPIO.output(8, GPIO.LOW) print('The LED has been turned OFF.') else: print("Please enter a valid value.") msg.delete()
class queueRead(): def __init__(self): ### initialize variables self.topic_vel = os.environ["TOPIC_VELOCITY"] self.topic_stop = os.environ["TOPIC_STOP"] ### get parameters #### self.SAS_name = os.environ["SAS_NAME"] self.SAS_value = os.environ["SAS_VALUE"] self.namespace = os.environ["TOPIC_NAMESPACE"] self.bot_id = os.environ["BOT_ID"] self.LGPWM = os.getenv('LGPWM', int(12)) self.LGIN1 = os.getenv('LGIN1', int(5)) self.LGIN2 = os.getenv('LGIN2', int(6)) self.RGPWM = os.getenv('RGPWM', int(13)) self.RGIN1 = os.getenv('RGIN1', int(16)) self.RGIN2 = os.getenv('RGIN2', int(26)) self.bus_service = ServiceBusService( service_namespace=self.namespace, shared_access_key_name=self.SAS_name, shared_access_key_value=self.SAS_value) self.bus_service.create_subscription((self.topic_vel + self.bot_id), self.bot_id) self.bus_service.create_subscription((self.topic_stop + self.bot_id), self.bot_id) ### setup ### GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(self.LGPWM, GPIO.OUT) GPIO.setup(self.LGIN1, GPIO.OUT) GPIO.setup(self.LGIN2, GPIO.OUT) self.Lpwm = GPIO.PWM(self.LGPWM, 1000) self.Lpwm.start(0) GPIO.setup(self.RGPWM, GPIO.OUT) GPIO.setup(self.RGIN1, GPIO.OUT) GPIO.setup(self.RGIN2, GPIO.OUT) self.Rpwm = GPIO.PWM(self.RGPWM, 1000) self.Rpwm.start(0) @staticmethod def motorCall(Gpwm, GIN1, GIN2, msg): if (msg > 0): GPIO.output(GIN1, GPIO.HIGH) GPIO.output(GIN2, GPIO.LOW) else: GPIO.output(GIN1, GPIO.LOW) GPIO.output(GIN2, GPIO.HIGH) pwm_per = abs(msg) Gpwm.ChangeDutyCycle(pwm_per) def motorStop(self): self.Lpwm.ChangeDutyCycle(0) self.Rpwm.ChangeDutyCycle(0) GPIO.output(self.LGIN1, GPIO.LOW) GPIO.output(self.LGIN2, GPIO.LOW) GPIO.output(self.RGIN1, GPIO.LOW) GPIO.output(self.RGIN2, GPIO.LOW) def read(self): while (1): try: msg = self.bus_service.receive_subscription_message( (self.topic_vel + self.bot_id), self.bot_id, peek_lock=False) if msg.body is not None: res = list(map(int, msg.body.split(' '))) queueRead.motorCall(self.Lpwm, self.LGIN1, self.LGIN2, res[0]) queueRead.motorCall(self.Rpwm, self.RGIN1, self.RGIN2, res[1]) msg = self.bus_service.receive_subscription_message( (self.topic_stop + self.bot_id), self.bot_id, peek_lock=False) if msg.body is not None: self.motorStop() except: pass
mongo_conn_str = os.getenv('PROD_MONGODB') shared_access_key = os.getenv('shared_access_key') shared_access_value = os.getenv('shared_access_value') service_namespace = os.getenv('service_namespace') print(service_namespace) bus_service = ServiceBusService(service_namespace=service_namespace, shared_access_key_name=shared_access_key, shared_access_key_value=shared_access_value) topic_name = "on-prem-test" # the topic name already defined in azure subscription = "heroku_consumer" # unique name for this application/consumer bus_service.create_topic(topic_name) # create subscription bus_service.create_subscription(topic_name, subscription) # get subscription message msg = bus_service.receive_subscription_message( topic_name, subscription, peek_lock=False) msg_body = None entity = None if msg.body is None: print('No messages to retrieve') exit() else: msg_body = msg.body.decode("utf-8") entity = json.loads(msg_body) print(msg_body) try: # write to DB mClient = MongoClient(mongo_conn_str) # get db db = mClient.neiss_test