1
#!/usr/bin/env python3
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from random import random

#Show Images
image_show = True

#Parameters ( mm )
square_size = 50
room_width = 5000
room_length = 10000
n_guess = 50 # number of random points generated
max_cell_value = 255 # highest number in a cell.

#Handy functions for conversions
def mm2grid( mm):
    return int(mm/square_size)
def grid2mm( grid):
    return int(grid*square_size)
def rotation( theta):
    return np.array([[np.cos(theta),-np.sin(theta)],[np.sin(theta),np.cos(theta)]])

# Build a simple empty map
map_floor = np.zeros((mm2grid(room_width), mm2grid(room_length)),)
# NOTE: Any cell >0 is considered occupied

print("Map size= {} cells".format( map_floor.shape))

# Set border walls
map_floor[ 0, :] = 255
map_floor[-1, :] = 255
map_floor[ :, 0] = 255
map_floor[ :,-1] = 255

#Draw something in the centre
map_floor[20:80,100:120] = 100

### Target Position ###
target = np.array([2500,0])

### Robot Position ###
x = np.array([1000,1000, np.pi*0])

# Randomly generate n_number of positions #
x_rand = np.zeros(( 3, n_guess))
for k in range( n_guess):
    x_rand[:,k] = np.array([ random()*room_width, random()* room_length, random()*3.14159]) 
    #remove any points inside a solid object
    if map_floor[ int(mm2grid( x_rand[0,k])), int(mm2grid( x_rand[1,k]))] > 0:
        x_rand[:,k] = 0

### TODO ###
#for n_number calculate angle to target#
#for n_number calculate distance measurement to nearest wall.

#### Display Map ####
# Remember: images are printed y inverted and x first.
if image_show:
    # Draw Map
    plt.matshow( max_cell_value-map_floor, cmap=plt.cm.gray) # max_cell_value is just to correct color scheme
    # Draw randomly positioned squares on the map
    plt.plot( [mm2grid( x_rand[1,:])], [mm2grid(x_rand[0,:])],'rx')
    # Draw Robot position
    plt.plot( [mm2grid( x[1])], [mm2grid(x[0])], 'b8', markersize=12)
    plt.plot( [mm2grid( x[1]), mm2grid( x[1]+ 300*np.cos(x[2]))],\
              [mm2grid( x[0]), mm2grid( x[0]+ 300*np.sin(x[2]))], 'b-', linewidth=2.5)
    plt.text( mm2grid( x[1]), mm2grid( x[0]+300), 'Robot', color='b')
    # Draw target
    plt.plot( [mm2grid( target[1])],[mm2grid( target[0])], 'g>', markersize=30)
    plt.text( mm2grid( target[1]), mm2grid( target[0] - 250), 'Target', color='g')
    # Draw line from robot to target
    plt.plot( [mm2grid( x[1]), mm2grid(target[1])],\
              [mm2grid( x[0]), mm2grid(target[0])], 'k--')
    # Show everything.
    plt.show()

I am not sure how to fix the error. in line 48 I changed xrange to range I also did some other things to some other places to fix and error I was getting in line 26 and it was saying

TypeError: 'float' object cannot be interpreted as an index

But it looks like I have fixed those problems but I am not sure if I have messed something up somewhere and now it is giving me this error:

Map size= (100, 200) cells
Traceback (most recent call last):
  File "/Users/oscarwallace/Downloads/Map.py", line 64, in <module>
    plt.plot( [mm2grid( x_rand[1,:])], [mm2grid(x_rand[0,:])],'rx')
  File "/Users/oscarwallace/Downloads/Map.py", line 19, in mm2grid
    return int(mm/square_size)
TypeError: only size-1 arrays can be converted to Python scalars

I was wondering if anyone knew what I have to do to sop this from happening. Thanks in advance.

1 Answer 1

1

This error occurs when you're trying to cast into an integer something that isn't just one scalar. For instance, a ndarray with two elements. This is an example of this error:

import numpy as np

int(np.array([1, 3]))

TypeError: only size-1 arrays can be converted to Python scalars

What you can do is use .astype(int)

(mm/square_size).astype(int)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.