def gen_magnets(): return [ Box(mag=(500, 0, 0), dim=(10, 10, 10), pos=(0, 12, 0)), Box(mag=(0, 500, 0), dim=(10, 10, 10), pos=(10.392304845, -6, 0), angle=60, axis=(0, 0, 1)), Box(mag=(0, 0, 500), dim=(10, 10, 10), pos=(-10.392304845, -6, 0), angle=-60, axis=(0, 0, 1)) ]
def gen_magnets(mag_pos): magnets = [] magnets.append(Box(mag=(0, 0, -500), dim=(5, 5, 5), pos=(0, 0, 0))) magnets.append(Box(mag=(-500, 0, 0), dim=(5, 5, 5), pos=(-5, 5, 0))) magnets.append(Box(mag=(-500, 0, 0), dim=(5, 5, 5), pos=(-5, 0, 0))) magnets.append(Box(mag=(-500, 0, 0), dim=(5, 5, 5), pos=(-5, -5, 0))) magnets.append(Box(mag=(500, 0, 0), dim=(5, 5, 5), pos=(5, 0, 0))) magnets.append(Box(mag=(500, 0, 0), dim=(5, 5, 5), pos=(5, -5, 0))) magnets.append(Box(mag=(0, -500, 0), dim=(5, 5, 5), pos=(0, -5, 0))) magnets.append(Box(mag=(500, 0, 0), dim=(5, 5, 5), pos=(5, 5, 0))) magnets.append(Box(mag=(0, 500, 0), dim=(5, 5, 5), pos=(0, 5, 0))) return magnets
def PlaceMagnets(self): self.magnets = [] self.sensors = [] init_linspace = np.linspace(0,self.chain._segment_count-1,num=self.chain._segment_count) init_pose = self.chain.GetPoints(init_linspace) init_orient = self.chain.GetOrientations(init_linspace) final_linspace = np.linspace(1,self.chain._segment_count,num=self.chain._segment_count) final_pose = self.chain.GetPoints(final_linspace) final_orient = self.chain.GetOrientations(final_linspace) for i in range(self.chain._segment_count): #Put sensor at end of line segment if(isinstance(self.chain._segments[i],LineSegment)): self.sensors.append(magpy.Sensor(pos=init_pose[i],axis=[0,1,0],angle=90)) rotvec_orient = init_orient[i].as_rotvec() if np.array_equal(init_orient[i].apply([1.,0.,0.]),[1.,0.,0.]) is False and np.sum(np.absolute(rotvec_orient)) is not 0: self.sensors[len(self.sensors)-1].rotate(axis=rotvec_orient,angle=(np.linalg.norm(rotvec_orient)*180)/np.pi) f #Put magnet at end of chain if(isinstance(self.chain._segments[i],CircleSegment)): self.magnets.append(Box(mag=self.magnetization,dim=self.dimension,pos=final_pose[i],axis=[0,1,0],angle=90)) rotvec_orient = final_orient[i].as_rotvec() if np.array_equal(final_orient[i].apply([1.,0.,0.]),[1.,0.,0.]) is False and np.sum(np.absolute(rotvec_orient)) is not 0: self.magnets[len(self.magnets)-1].rotate(axis=rotvec_orient,angle=(np.linalg.norm(rotvec_orient)*180)/np.pi) self.collection = magpy.Collection(self.magnets) return self.collection
def gen_magnets(self, field_strength, pos, pos_offset, angle, angle_offset): magnets = [] i = 0 for field in field_strength: magnet = Box(mag=(0,0,field), \ dim=(self.balljoint_config['magnet_dimension'][i][0],self.balljoint_config['magnet_dimension'][i][1],self.balljoint_config['magnet_dimension'][i][2]),\ pos=(pos[i][0]+pos_offset[i][0],pos[i][1]+pos_offset[i][1],pos[i][2]+pos_offset[i][2])) magnet.rotate(angle=angle[i][0] + angle_offset[i][0], axis=(1, 0, 0)) magnet.rotate(angle=angle[i][1] + angle_offset[i][1], axis=(0, 1, 0)) magnet.rotate(angle=angle[i][2] + angle_offset[i][2], axis=(0, 0, 1)) magnets.append(magnet) i = i + 1 return magnets
def gen_magnets_angle(self, angles): magnets = [] for field,mag_dim,pos,angle in zip(self.config['field_strength'],\ self.config['magnet_dimension'],self.config['magnet_pos'],angles): magnet = Box(mag=(0,0,field), dim=mag_dim,\ pos=(pos[0],pos[1],pos[2])) magnet.rotate(angle=angle[0], axis=(1, 0, 0)) magnet.rotate(angle=angle[1], axis=(0, 1, 0)) magnet.rotate(angle=angle[2], axis=(0, 0, 1)) magnets.append(magnet) return Collection(magnets)
def gen_magnets(field_strength, mag_dim, pos, pos_offset, angle, angle_offset): magnets = [] i = 0 for field in field_strength: magnet = Box(mag=(0,0,field), \ dim=mag_dim[i],\ pos=(pos[i][0]+pos_offset[i][0],pos[i][1]+pos_offset[i][1],pos[i][2]+pos_offset[i][2])) magnet.rotate(angle=angle[i][0] + angle_offset[i][0], axis=(1, 0, 0)) magnet.rotate(angle=angle[i][1] + angle_offset[i][1], axis=(0, 1, 0)) magnet.rotate(angle=angle[i][2] + angle_offset[i][2], axis=(0, 0, 1)) magnets.append(magnet) i = i + 1 return magnets
def gen_magnets(self): magnets = [] for field,mag_dim,pos,pos_offset,angle,angle_offset in zip(self.config['field_strength'],\ self.config['magnet_dimension'],self.config['magnet_pos'],self.config['magnet_pos_offsets'],\ self.config['magnet_angle'],self.config['magnet_angle_offsets']): magnet = Box(mag=(0,0,field), dim=mag_dim,\ pos=(pos[0]+pos_offset[0],\ pos[1]+pos_offset[1],\ pos[2]+pos_offset[2])\ ) magnet.rotate(angle=angle[0] + angle_offset[0], axis=(1, 0, 0)) magnet.rotate(angle=angle[1] + angle_offset[1], axis=(0, 1, 0)) magnet.rotate(angle=angle[2] + angle_offset[2], axis=(0, 0, 1)) magnets.append(magnet) return Collection(magnets)
def gen_magnets_all(self, field_strength, positions, position_offsets, angles, angle_offsets): magnets = [] i = 0 for field,mag_dim,pos,pos_offset,angle,angle_offset in zip(field_strength,\ self.config['magnet_dimension'],positions,position_offsets,\ angles,angle_offsets): magnet = Box(mag=(0,0,field), dim=mag_dim,\ pos=(pos[0]+pos_offset[0],\ pos[1]+pos_offset[1],\ pos[2]+pos_offset[2])\ ) magnet.rotate(angle=angle[0] + angle_offset[0], axis=(1, 0, 0)) magnet.rotate(angle=angle[1] + angle_offset[1], axis=(0, 1, 0)) magnet.rotate(angle=angle[2] + angle_offset[2], axis=(0, 0, 1)) magnets.append(magnet) i = i + 1 return Collection(magnets)
def gen_magnets(mag_pos): magnets = [] for i in range(0,dimx): for j in range(0,dimy): for k in range(0,dimz): if mag_pos[i*dimx+j*dimy+k*dimz]==1: magnets.append(Box(mag=(field_strength,0,0),dim=(cs,cs,cs),pos=((i-dimx//2)*(cs+1),(j-dimy//2)*(cs+1),(k-dimz//2)*(cs+1)))) if mag_pos[i*dimx+j*dimy+k*dimz]==2: magnets.append(Box(mag=(-field_strength,0,0),dim=(cs,cs,cs),pos=((i-dimx//2)*(cs+1),(j-dimy//2)*(cs+1),(k-dimz//2)*(cs+1)))) if mag_pos[i*dimx+j*dimy+k*dimz]==3: magnets.append(Box(mag=(0,field_strength,0),dim=(cs,cs,cs),pos=((i-dimx//2)*(cs+1),(j-dimy//2)*(cs+1),(k-dimz//2)*(cs+1)))) if mag_pos[i*dimx+j*dimy+k*dimz]==4: magnets.append(Box(mag=(0,-field_strength,0),dim=(cs,cs,cs),pos=((i-dimx//2)*(cs+1),(j-dimy//2)*(cs+1),(k-dimz//2)*(cs+1)))) if mag_pos[i*dimx+j*dimy+k*dimz]==5: magnets.append(Box(mag=(0,0,field_strength),dim=(cs,cs,cs),pos=((i-dimx//2)*(cs+1),(j-dimy//2)*(cs+1),(k-dimz//2)*(cs+1)))) if mag_pos[i*dimx+j*dimy+k*dimz]==6: magnets.append(Box(mag=(0,0,-field_strength),dim=(cs,cs,cs),pos=((i-dimx//2)*(cs+1),(j-dimy//2)*(cs+1),(k-dimz//2)*(cs+1)))) return magnets
dis = 10 # magnet size and field mag_size_x = 1 mag_size_y = 4 mag_size_z = 2 mag_field_x = 1300 mag_field_y = 0 mag_field_z = 0 pos_x = (dis + mag_size_x/2*2)/2 pos_z = - mag_size_z/2 # create magnets sL = Box(mag=(mag_field_x,mag_field_y,mag_field_z), dim=(mag_size_x,mag_size_y,mag_size_z), pos=(-pos_x,0,pos_z)) sL_1 = Box(mag=(mag_field_x,mag_field_y,mag_field_z), dim=(mag_size_x,mag_size_y,mag_size_z), pos=(-pos_x,0,pos_z)) sL_2 = Box(mag=(mag_field_x,mag_field_y,mag_field_z), dim=(mag_size_x,mag_size_y,mag_size_z), pos=(-pos_x,0,pos_z)) sR = Box(mag=(mag_field_x,mag_field_y,mag_field_z), dim=(mag_size_x,mag_size_y,mag_size_z), pos=(pos_x,0,pos_z)) sR_1 = Box(mag=(mag_field_x,mag_field_y,mag_field_z), dim=(mag_size_x,mag_size_y,mag_size_z), pos=(pos_x,0,pos_z)) sR_2 = Box(mag=(mag_field_x,mag_field_y,mag_field_z), dim=(mag_size_x,mag_size_y,mag_size_z), pos=(pos_x,0,pos_z)) #move copy bos sL_1.move((-mag_size_x,0,0)) sR_1.move((mag_size_x,0,0)) sL_2.move((-2*mag_size_x,0,0)) sR_2.move((2*mag_size_x,0,0)) # create collection c = Collection(sL,sL_1,sL_2,sR,sR_1,sR_2)
import magpylib as magpy from magpylib.source.magnet import Box #fixed magnet parameters M = [0,0,1] #magnetization D = [3,3,3] #dimension #rotation axis rax = [-1,1,-1] #magnets with different orientations s1 = Box(mag=M, dim=D, pos=[-6,0,4], angle=0, axis=rax) s2 = Box(mag=M, dim=D, pos=[ 0,0,4], angle=45, axis=rax) s3 = Box(mag=M, dim=D, pos=[ 6,0,4], angle=90, axis=rax) #magnets that are rotated differently s4 = Box(mag=M, dim=D, pos=[-6,0,-4]) s5 = Box(mag=M, dim=D, pos=[ 0,0,-4]) s5.rotate(45,rax) s6 = Box(mag=M, dim=D, pos=[ 6,0,-4]) s6.rotate(90,rax) #collect all c = magpy.Collection(s1,s2,s3,s4,s5,s6) #display collection fig = c.displaySystem() fig.set_size_inches(6, 6)
from magpylib.source.magnet import Box import magpylib as magpy from numpy import array #fixed magnet parameters M = [0, 0, 1] #magnetization D = [2, 2, 2] #dimension # Translation of magnets can be realized in several ways s1 = Box(mag=M, dim=D, pos=[-4, 0, 4]) s2 = Box(mag=M, dim=D, pos=[-2, 0, 4]) s2.move([0, 0, -2]) s3 = Box(mag=M, dim=D, pos=[0, 0, 4]) s3.move([0, 0, -2]) s3.move([0, 0, -2]) s4 = Box(mag=M, dim=D, pos=[2, 0, 4]) s4.setPosition([2, 0, -2]) s5 = Box(mag=M, dim=D, pos=[4, 0, 4]) s5.position = array([4, 0, 0]) #collection c = magpy.Collection(s1, s2, s3, s4, s5) #display collection fig = c.displaySystem() fig.set_size_inches(6, 6)
def getB(phi, th, psi): pm = Box(mag=mag, dim=dim, pos=posM) axis = axisFromAngles([psi, 90]) pm.rotate(phi, [0, 0, 1], anchor=[0, 0, 0]) pm.rotate(th, axis, anchor=anch) return pm.getB(posS)
#All sensor readings for each timestep sensor_readings = [] for j in range(len(timesteps)): #Get all data for timestep timestep_data = data[data['time'].isin([timesteps[j]])] #Separate sensors and magnets for timestep sensors = timestep_data[timestep_data['joint_index'].isin(np.arange(0,joints,1))] magnets = timestep_data[timestep_data['joint_index'].isin(np.arange(0,joints,1))] b = [] for i in range(len(magnets['angle'])): if(np.array_equal(list(magnets['axis'])[i],[0.0,0.0,0.0])): b.append(Box(mag=mag,dim=dim,pos=[list(magnets['x'])[i],list(magnets['y'])[i],list(magnets['z'])[i]])) else: b.append(Box(mag=mag,dim=dim,pos=[list(magnets['x'])[i],list(magnets['y'])[i],list(magnets['z'])[i]],angle=list(magnets['angle'])[i],axis=list(magnets['axis'])[i])) col = Collection(b) s = [] for i in range(len(sensors['angle'])): if(np.array_equal(list(sensors['axis'])[i],[0.0,0.0,0.0])): sensor = magpy.Sensor(pos=[list(sensors['x'])[i],list(sensors['y'])[i],list(sensors['z'])[i]]) else: sensor = magpy.Sensor(pos=[list(sensors['x'])[i],list(sensors['y'])[i],list(sensors['z'])[i]],axis=list(sensors['axis'])[i],angle=list(sensors['angle'])[i]) s.append(sensor) reading = np.round(sensor.getB(col),4) sensor_readings.append(reading)
# taken from https://magpylib.readthedocs.io/en/latest/_pages/2_guideExamples/ from magpylib.source.magnet import Box import magpylib as magpy # fixed magnet parameters M = [0, 0, 1] #magnetization D = [4, 2, 2] #dimension # magnets with Euler angle orientations s1 = Box(mag=M, dim=D, pos=[-4, 0, 4]) s2 = Box(mag=M, dim=D, pos=[4, 0, 4], angle=45, axis=[0, 0, 1]) s3 = Box(mag=M, dim=D, pos=[-4, 0, -4], angle=45, axis=[0, 1, 0]) s4 = Box(mag=M, dim=D, pos=[4, 0, -4], angle=45, axis=[1, 0, 0]) # collection c = magpy.Collection(s1, s2, s3, s4) # display collection magpy.displaySystem(c, direc=True, figsize=(6, 6))
x.resample() x.update() pos.text(x.best_pos) if graph_sensor == True: #Save best filter data best_data.append(x.sensor_data) #Update filter data plot if i % 10 == 0 and i > 100: data_plot.line_chart(best_data[-100:-1]) if mode == "Training Mode": #Plot real sensor data real_data_plot.line_chart(sensor_data[0:i]) #Calculate and plot error data_difference.append(np.subtract(sensor_data[i],x.best_data[0])) error_data_plot.line_chart(data_difference) if graph_pose == True: magnet = Box(mag=[0,0,1],dim=[6.35,6.35,6.35],pos=x.best_pos[0],angle=x.best_angle[0],axis=x.best_axis[0]) magpy.displaySystem(magnet,suppress=True) pos_plot.pyplot() plt.close() if mode == "Training Mode": if graph_pose_error == True: pos_difference.append(np.linalg.norm(np.subtract(x.best_pos[0],magnet_pos[i]))) error_plot.line_chart(pos_difference) i = i+1
from magpylib import source, Collection from scipy.spatial.transform import Rotation as R from matplotlib import pyplot as plt data = pickle.load(open("data/test.p", "rb")) mag = [0, 0, -575.4] dim = [6.35, 6.35, 6.35] sen = [] loops = 150 for i in range(5, loops): b = Box(mag=mag, dim=dim, pos=data[i][0], angle=data[i][1], axis=data[i][2]) sen.append(magpy.Sensor(pos=data[i][0], angle=data[i][1], axis=data[i][2])) col = Collection(b) if (i == (loops - 1)): h = 1 magpy.displaySystem(col, sensors=sen) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') length = 3 for i in range(5, loops): x = [1, 0, 0] y = [0, 1, 0]
# taken from https://magpylib.readthedocs.io/en/latest/_pages/2_guideExamples/ import numpy as np import matplotlib.pyplot as plt from magpylib.source.magnet import Box, Cylinder from magpylib import Collection, displaySystem # create magnets s1 = Box(mag=(0, 0, 600), dim=(3, 3, 3), pos=(-4, 0, 3)) s2 = Cylinder(mag=(0, 0, 500), dim=(3, 5)) # create collection c = Collection(s1, s2) # manipulate magnets individually s1.rotate(45, (0, 1, 0), anchor=(0, 0, 0)) s2.move((5, 0, -4)) # manipulate collection c.move((-2, 0, 0)) # calculate B-field on a grid xs = np.linspace(-10, 10, 33) zs = np.linspace(-10, 10, 44) POS = np.array([(x, 0, z) for z in zs for x in xs]) Bs = c.getB(POS).reshape(44, 33, 3) #<--VECTORIZED # create figure fig = plt.figure(figsize=(9, 5)) ax1 = fig.add_subplot(121, projection='3d') # 3D-axis ax2 = fig.add_subplot(122) # 2D-axis
import magpylib as magpy from magpylib.source.magnet import Box import matplotlib.pyplot as plt # define figure fig = plt.figure(figsize=(6,6)) ax = fig.add_subplot(1,1,1, projection='3d') # fixed magnet parameters M = [0,0,1] #magnetization D = [2,4,1] #dimension # define magnets rotated with different pivot and anchor points piv1 = [-7,0,5] s1 = Box(mag=M, dim=D, pos = [-7,-3,5]) piv2 = [0,0,5] s2 = Box(mag=M, dim=D, pos = [0,-3,5]) s2.rotate(-30,[0,0,1],anchor=piv2) piv3 = [7,0,5] s3 = Box(mag=M, dim=D, pos = [7,-3,5]) s3.rotate(-60,[0,0,1],anchor=piv3) piv4 = [-7,0,-5] anch4 = [-7,0,-2] s4 = Box(mag=M, dim=D, pos = [-7,-3,-5]) piv5 = [0,0,-5] anch5 = [0,0,-2] s5 = Box(mag=M, dim=D, pos = [0,-3,-5])
import numpy as np import matplotlib.pyplot as plt from magpylib.source.magnet import Box from magpylib import Collection #create collection of four magnets s1 = Box(mag=[500, 0, 500], dim=[3, 3, 3], pos=[0, 0, 3], angle=45, axis=[0, 1, 0]) s2 = Box(mag=[-500, 0, -500], dim=[3, 3, 3], pos=[0, 0, -3], angle=45, axis=[0, 1, 0]) s3 = Box(mag=[500, 0, -500], dim=[4, 4, 4], pos=[4, 0, 0], angle=45, axis=[0, 1, 0]) s4 = Box(mag=[-500, 0, 500], dim=[4, 4, 4], pos=[-4, 0, 0], angle=45, axis=[0, 1, 0]) c = Collection(s1, s2, s3, s4) #create positions xs = np.linspace(-8, 8, 100) zs = np.linspace(-8, 8, 100)
from magpylib.source.magnet import Box, Cylinder, Sphere # Simuate a N38 5 * 5 *5 mm #https://www.leeyed.com/rare_earth_magnet.html s_Box = Box(mag=(0, 0, 1220), dim=(5, 5, 5), pos=(0, 0, -2.5)) s_Cylinder = Cylinder(mag=(0, 0, 1220), dim=(5, 5), pos=(0.0, 0.0, -2.5), angle=0.0, axis=(0.0, 0.0, 1.0), iterDia=50) s_Sphere = Sphere(mag=(0.0, 0.0, 1220), dim=5.0, pos=(0.0, 0.0, -2.5), angle=0.0, axis=(0.0, 0.0, 1.0)) print(s_Box.getB([0, 0, 5])) print(s_Cylinder.getB([0, 0, 5])) print(s_Sphere.getB([0, 0, 5]))
from magpylib.source.magnet import Box from numpy import linspace import matplotlib.pyplot as plt #create magnet pm = Box(mag=[0, 0, 1], dim=[1, 1, 1]) #create sensor positions posis = [[x, 0, 3] for x in linspace(-10, 10, 100)] #calcualte fields Bs = pm.getBsweep(posis) #plot fields plt.plot(Bs)
" " + str(rot[2] / 180.0 * math.pi) + "\n") if first: # create figure fig = plt.figure(figsize=(18, 7)) ax1 = fig.add_subplot(131, projection='3d') # 3D-axis ax2 = fig.add_subplot(132) # 2D-axis ax3 = fig.add_subplot(133) # 2D-axis Bs = c.getB(POS0).reshape(44, 33, 3) #<--VECTORIZED X, Y = np.meshgrid(xs, ys) U, V = Bs[:, :, 0], Bs[:, :, 2] ax2.streamplot(X, Y, U, V, color=np.log(U**2 + V**2)) Bs = c.getB(POS1).reshape(44, 33, 3) #<--VECTORIZED X, Z = np.meshgrid(xs, zs) U, V = Bs[:, :, 0], Bs[:, :, 2] ax3.streamplot(X, Z, U, V, color=np.log(U**2 + V**2)) sensor_visualization = [] i = 0 for pos in sensor_pos: sensor_visualization.append( Box(mag=(0, 0, 0.001), dim=(1, 1, 1), pos=sensor_pos[i])) i = i + 1 d = Collection(c, sensor_visualization) displaySystem(d, subplotAx=ax1, suppress=True) plt.show() first = False if iter % 10 == 0: print("(%d/%d)" % (iter, iterations)) record.close()