I have some issues on float conversion: Here is my code and Error message, need urgent help
--------------------------------------------------------------------------------------------------------------------------------------
Error Message
Traceback (most recent call last):
File "stepper_sensor.py", line 146, in <module>
forward(float(delay) / 1000.0, int(steps))
File "stepper_sensor.py", line 104, in forward
r, theta, z = mapping(lDistances,angle, z1)
File "stepper_sensor.py", line 121, in mapping
r= float(lDistances*math.sin(angle))
TypeError: a float is required
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import RPi.GPIO as GPIO
import time
import os
import fileinput
import plotly.plotly as py
from plotly.graph_objs import *
import math
import array
username = 'pavi'
api_key = 'oegyhub3vl'
stream_token = '2idjptldya'
py.sign_in(username, api_key)
GPIO.setmode(GPIO.BCM)
enable_pin = 18
coil_A_1_pin = 4
coil_A_2_pin = 2
coil_B_1_pin = 24
coil_B_2_pin = 25
GPIO_TRIGGER = 15
GPIO_ECHO = 14
GPIO.setup(enable_pin, GPIO.OUT)
GPIO.setup(coil_A_1_pin, GPIO.OUT)
GPIO.setup(coil_A_2_pin, GPIO.OUT)
GPIO.setup(coil_B_1_pin, GPIO.OUT)
GPIO.setup(coil_B_2_pin, GPIO.OUT)
print "Ultrasonic Measurement"
# Set pins as output and input
GPIO.setup(GPIO_TRIGGER,GPIO.OUT) # Trigger
GPIO.setup(GPIO_ECHO,GPIO.IN) # Echo
# Set trigger to False (Low)
GPIO.output(GPIO_TRIGGER, False)
GPIO.output(enable_pin, 1)
def forward(delay, steps):
angle = []
#Create a list to store distances
lDistances = []
z1 = []
for i in range(1, steps):
degree= i/1.422
# print '{:1.1f}'.format(degree)
setStep(1, 0, 1, 0)
time.sleep(delay)
setStep(0, 1, 1, 0)
time.sleep(delay)
setStep(0, 1, 0, 1)
time.sleep(delay)
setStep(1, 0, 0, 1)
time.sleep(delay)
angle.append(degree)
with open('angle.txt','a') as file:
for item in angle:
file.write("{:1.1f}\n".format(item))
time.sleep(0.3)
# Send 10us pulse to trigger
GPIO.output(GPIO_TRIGGER, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
start = time.time()
while GPIO.input(GPIO_ECHO)==0:
start = time.time()
while GPIO.input(GPIO_ECHO)==1:
stop = time.time()
# Calculate pulse length
elapsed = stop-start
# Distance pulse travelled in that time is time
# multiplied by the speed of sound (cm/s)
distance = elapsed * 34300
# That was the distance there and back so halve the value
distance = distance / 2
print "Distance : %.1f" % distance+ " " + '{:1.1f}'.format(degree)+ " "+ '{:1.1f}'.format(count1)
if i > 0 and distance > 0:
lDistances.append(distance)
with open('distances.txt','a') as file:
for item in lDistances:
file.write("{:1.1f}\n".format(item))
z1.append(count1)
with open('z1.txt','a') as file:
for item in z1:
file.write("{:1.1f}\n".format(item))
r, theta, z = mapping(lDistances,angle, z1)
trace0 = Scatter3d(x= r, y= theta, z=z1)
layout = Layout(
title='Raspberry Pi Streaming Sensor Data'
)
fig = Figure(data=[trace0], layout=layout)
plot_url = py.plot(fig)
def mapping(lDistances,angle, z1):
r= float(lDistances*math.sin(angle))
theta = distance*math.cos(angle)
z = float(count1)
return r, theta, z
def setStep(w1, w2, w3, w4):
GPIO.output(coil_A_1_pin, w1)
GPIO.output(coil_A_2_pin, w2)
GPIO.output(coil_B_1_pin, w3)
GPIO.output(coil_B_2_pin, w4)
count1 = 0
while True:
r = raw_input("do you want to run (y/n): ")
count1 += 1
count = 0
while (count <1):
if r == 'y':
delay = 10.0
steps = 512.0
forward(float(delay) / 1000.0, int(steps))
count = count + 1
# Reset GPIO settings
GPIO.cleanup()