add bode_plot to shapepath.py

This commit is contained in:
2018-10-30 16:57:58 +01:00
parent 9c8e2a417e
commit dd408e6956
2 changed files with 149 additions and 107 deletions

View File

@@ -30,7 +30,7 @@ from scipy import signal
sys.path.insert(0,os.path.expanduser('~/Documents/prj/SwissFEL/PBTools/'))
#import pbtools.misc.pp_comm as pp_comm -> pp_comm.PPComm
from pbtools.misc.pp_comm import PPComm
from pbtools.misc.pp_comm import PPComm,GpasciiChannel
from pbtools.misc.gather import Gather
from pbtools.misc.tuning import Tuning
@@ -44,6 +44,7 @@ class MXTuning(Tuning):
def init_stage(self):
comm=self.comm
if comm is None: return
gpascii=comm.gpascii
sys.stdout.write('homing stage');sys.stdout.flush()
gpascii.send_line('enable plc1')
@@ -201,100 +202,6 @@ class MXTuning(Tuning):
# tp print see also: print(np.poly1d([1,2,3], variable='s')), print(np.poly1d([1,2,3], r=True, variable='s'))
def custom_chirp(self):
motor = 1
amp, minFrq, maxFrq, tSec = (10, 10, 300, 30)
file='/tmp/gather.npz'
# if not os.path.isfile(f): tune.init_stage();plt.close('all')
# tune.bode_chirp(openloop=True, file=f, motor=mot, amp=amp, minFrq=minFrq, maxFrq=maxFrq, tSec=tSec)
prog = '''
&0 //cout works only in coord 0
open prog 999
L11=0
L10=0
L12=0
L13=0
L14=0
Gather.Enable=2
while(L10<300005)
{
L12=10*sin(31.415926535897931*(pow(1.058324104020218,(L10*0.000199996614513))-1)/log(1.058324104020218))
cout%d:(L12)
L10=L10+1
}
Gather.Enable=0
close
b999r
'''%motor
gpascii = self.comm.gpascii
gt = self.gather
print(gpascii.servo_period)
gt.set_phasemode(False)
address=("Motor[1].IqCmd", "Motor[1].ActPos",)
gt.set_address(*address)
#Gather.Enable=1
gt.set_property(MaxSamples=300000, Period=1)
# gt.enable(2)
gpascii.send_line(prog)
gpascii.sync()
gt.wait_stopped()
self.data=data=gt.upload()
meta={'motor':motor,'date':time.asctime(),'minFrq':minFrq,'maxFrq':maxFrq,'tSec':tSec,'amp':amp,'address':address}
np.savez_compressed(file, data=data, meta=meta)
meta['file'] = file
self.bode_chirp_plot(data, meta,True)
pass
def bode_sine(self,openloop=True,motor=1,minFrq=1,maxFrq=20,numFrq=15,amp=10,file='/tmp/gather.npz'):
'''calculates phase and amplitude at different frequencies and
saves:#loads and plots the bode diagram'''
if False:# os.path.isfile(file):
f=np.load(file)
bode=f['bode']
meta=f['meta'].item()
meta['file']=file
else:
gpascii=self.comm.gpascii
#motor 1 maximum: 13750
#amp= percentage of maximum amplitude
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
frqLst=np.logspace(np.log10(minFrq),np.log10(maxFrq),numFrq)
n=len(frqLst)
#frqLst=(10,15,20,25,30)
bode=np.ndarray((n,3))
bode[:, 0]=frqLst
#for i in range(n):
for i in range(n-1,-1,-1):
frq=frqLst[i]
t=1
rep=max(1,frq*t)
if openloop:
data=self.do_command('openloopsine',motor,amp,frq,rep,0)
else:
data=self.do_command('sinusoidal',motor,amp,frq,rep,0)
data=data[:,(1,2)]
gpascii.send_line('#1j=0')
time.sleep(1)
ax.clear()
avg=data.mean(0)
print(avg)
ax.plot(data[:, 0]-avg[0] , 'b-', label='input')
ax.plot(data[:, 1]-avg[1], 'g-', label='output')
#plt.pause(.05)
bode[i,1:]=self.phase_amp(frq, rep)
print('frq %g ampl %g phase %g'%tuple(bode[i,:]))
plt.show(block=False);plt.pause(.05)
meta={'motor':motor,'date':time.asctime()}
np.savez_compressed(file, bode=bode, meta=meta)
meta['file']=file
self.bode_sine_plot(bode, meta)
def bode(mdl):
w,mag,phase = signal.bode(mdl,1000)
f=w/(2*np.pi)
@@ -315,7 +222,7 @@ if __name__=='__main__':
from argparse import ArgumentParser,RawDescriptionHelpFormatter
import logging
logger = logging.getLogger(__name__)
logger = logging.getLogger('pbtools.misc.pp_comm')
#logger = logging.getLogger('pbtools.misc.pp_comm')
logger.setLevel(logging.DEBUG)
logging.basicConfig(format=('%(asctime)s %(name)-12s '
'%(levelname)-8s %(message)s'),
@@ -342,10 +249,13 @@ Examples:'''+''.join(map(lambda s:cmd+s, exampleCmd))+'\n '
args=parser.parse_args()
#plt.ion()
#comm = PPComm(host=args.host)
#gt = Gather(comm)
#tune=MXTuning(comm,gt)
tune = MXTuning(None,None)
args.host='MOTTEST-CPPM-CRM0573'
if args.host is None:
comm=gt=None
else:
comm = PPComm(host=args.host)
gt = Gather(comm)
tune=MXTuning(comm,gt)
base='MXTuning'
if args.dir is not None:
base=args.dir
@@ -426,10 +336,12 @@ Examples:'''+''.join(map(lambda s:cmd+s, exampleCmd))+'\n '
tune.bode_chirp(openloop=False, file=fn)
print('done')
elif mode==7: #further tests
tune.init_stage();
plt.close('all')
tune.bode_sine()
tune.custom_chirp()
#tune.init_stage();
plt.close('all')
#tune.custom_chirp()
tune.custom_chirp(motor=1,minFrq=1,maxFrq=3000,tSec=5,mode=0,file='/tmp/cst_chirp0.npz')
tune.custom_chirp(motor=2,minFrq=1,maxFrq=1000,tSec=5,mode=1,file='/tmp/cst_chirp1.npz')
tune.custom_chirp(motor=1,minFrq=1,maxFrq=3000,tSec=5,mode=2,file='/tmp/cst_chirp2.npz')
plt.show()
#------------------ Main Code ----------------------------------

File diff suppressed because one or more lines are too long