Exercise Ultrasonic+ Servo

# Module Import

import LocoIOT

import time


# Class Instances Creation

loco_iot = LocoIOT.LocoIOT()

msg = LocoIOT.IOT_Codes()


# Initiate Connection to the Microcontroller

loco_iot.connect()


# Enable Ultrasonic Sensor Hardware Configuration

loco_iot.enable(msg.SUBTYPE_ULTRA)

loco_iot.enable(msg.SUBTYPE_SERVO)


# Start Listening for Control/Request Messages

loco_iot.start()


servo_position = 0


dist_list = {

}



for i in range(10) :

dist_dict = loco_iot.getData(msg.SUBTYPE_ULTRA)

dist_list = dist_dict['Distance (cm)']

time.sleep(.1)

# Map an Input Value to a Different Range of Values

def map_to_range(x, in_min, in_max, out_min, out_max):

return (x - 5) * (180 - 0) / (25 - 5) + 0


# Infinite Loop: Press STOP to end the loop.

while True:


# Request Distance Data

x = dist_dict['Distance (cm)']

dist_dict = loco_iot.getData(msg.SUBTYPE_ULTRA)

vay = map_to_range(x, 5, 25, 0, 180)

loco_iot.setData(msg.SUBTYPE_SERVO, [x])

print(dist_dict)

# Short Arbitrary Delay

time.sleep(0.1)

# Close Connection to the Microcontroller

loco_iot.close()