pavithan
Posts: 2
Joined: Thu Nov 12, 2015 5:06 am

Type Error: Float is required

Thu Nov 12, 2015 5:34 am

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()

User avatar
PeterO
Posts: 5951
Joined: Sun Jul 22, 2012 4:14 pm

Re: Type Error: Float is required

Thu Nov 12, 2015 10:04 am

Code: Select all


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):
  
  [b]r= float(lDistances*math.sin(angle))
  theta = distance*math.cos(angle)[/b]
  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()
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
PeterO
Posts: 5951
Joined: Sun Jul 22, 2012 4:14 pm

Re: Type Error: Float is required

Thu Nov 12, 2015 10:10 am

At first I thought the problem was that you are trying to convert a list into a float (which wouldn't work).
But on further looking I thing the problem is that you are trying to take the sine of a list.

I **THINK** you need some list comprehension in there to go through all the elements of the lists in turn and compute the sine and do the multiplication.

BUT list comprehensions are something that I'm not very good at so I'll wait for a better Python progrrammer to come along :-)

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Aydan
Posts: 729
Joined: Fri Apr 13, 2012 11:48 am
Location: Germany, near Lake Constance

Re: Type Error: Float is required

Thu Nov 12, 2015 10:29 am

List comprehension works like this:
e.g. calculate sines for a list of floats:

#this returns a list of the results:
result=[math.sin(x) for x in list_of_floats]
#this returns a generator object to be iterated over:
result=(math.sin(x) for x in list_of_floats)

or:
result=map(math.sin,list_of_floats)
# calls function math.sin for each element of list_of_floats

Reagrds
Aydan

pavithan
Posts: 2
Joined: Thu Nov 12, 2015 5:06 am

Re: Type Error: Float is required

Fri Nov 13, 2015 4:42 am

Aydan wrote:List comprehension works like this:
e.g. calculate sines for a list of floats:

#this returns a list of the results:
result=[math.sin(x) for x in list_of_floats]
#this returns a generator object to be iterated over:
result=(math.sin(x) for x in list_of_floats)

or:
result=map(math.sin,list_of_floats)
# calls function math.sin for each element of list_of_floats

Reagrds
Aydan
---------------------------------------------------------------------------------------------------------------------------
this what i tired but still have some error
def mapping(lDistances,angle, z1):

r= lDistances*[math.sin(angle)for angle in list_of_floats]
theta = lDistances*[math.cos(angle)for angle in list_of_floats]
z = (count1)

return r, theta, z
----------------------------------------------------------------------------------------------------
ERROR MSG
Traceback (most recent call last):
File "stepper_sensor.py", line 146, in <module>
float(forward(float(delay) / 1000.0))
File "stepper_sensor.py", line 104, in forward
r, theta, z = mapping(lDistances,angle, z1)
File "stepper_sensor.py", line 121, in mapping
r= lDistances*[math.sin(angle)for angle in list_of_floats]
NameError: global name 'list_of_floats' is not defined

User avatar
AndyD
Posts: 2334
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: Type Error: Float is required

Fri Nov 13, 2015 5:06 am

pavithan wrote:this what i tired but still have some error
def mapping(lDistances,angle, z1):

r= lDistances*[math.sin(angle)for angle in list_of_floats]
theta = lDistances*[math.cos(angle)for angle in list_of_floats]
z = (count1)

return r, theta, z
----------------------------------------------------------------------------------------------------
ERROR MSG
Traceback (most recent call last):
File "stepper_sensor.py", line 146, in <module>
float(forward(float(delay) / 1000.0))
File "stepper_sensor.py", line 104, in forward
r, theta, z = mapping(lDistances,angle, z1)
File "stepper_sensor.py", line 121, in mapping
r= lDistances*[math.sin(angle)for angle in list_of_floats]
NameError: global name 'list_of_floats' is not defined
I think you want:-

Code: Select all

r = [distance * math.sin(angle) for distance in lDistances]
theta = [distance * math.cos(angle) for distance in lDistances]

Aydan
Posts: 729
Joined: Fri Apr 13, 2012 11:48 am
Location: Germany, near Lake Constance

Re: Type Error: Float is required

Fri Nov 13, 2015 10:00 am

AndyD wrote:
pavithan wrote:this what i tired but still have some error
def mapping(lDistances,angle, z1):

r= lDistances*[math.sin(angle)for angle in list_of_floats]
theta = lDistances*[math.cos(angle)for angle in list_of_floats]
z = (count1)

return r, theta, z
----------------------------------------------------------------------------------------------------
ERROR MSG
Traceback (most recent call last):
File "stepper_sensor.py", line 146, in <module>
float(forward(float(delay) / 1000.0))
File "stepper_sensor.py", line 104, in forward
r, theta, z = mapping(lDistances,angle, z1)
File "stepper_sensor.py", line 121, in mapping
r= lDistances*[math.sin(angle)for angle in list_of_floats]
NameError: global name 'list_of_floats' is not defined
I think you want:-

Code: Select all

r = [distance * math.sin(angle) for distance in lDistances]
theta = [distance * math.cos(angle) for distance in lDistances]
AndyD is correct.
pavithan: I only gave you an example. You have to adapt it to you code. list_of_floats was only a placeholder for your actual list of floats.

Regards
Aydan

Return to “General discussion”