Bonjour à tous,
Je me demandais s'il y avais une librairie pour effectuer des rampes / automations en python.
Je pensais à une librairie en C afin d'avoir un maximum d'optimisation en terme de processeur.
J'ai essayé une bidouille en python, mais ca m'a pas l'air sérieux du tout… donc je me dis qu'il y a quelque chose qui doit exister.
une idée?
#! /usr/bin/env python # -*- coding: utf-8 -*- import threading from time import sleep import datetime class Ramp(threading.Thread): """Instanciate a thread for Playing a ramp Allow to do several ramps in a same scenario""" def __init__(self, origin, args): threading.Thread.__init__(self) self.args = args self.origin = origin self.start() def run(self): print('ramp starts in ' + self.name + ' at ' + str(datetime.datetime.now())) index = self.args.index('ramp') ramp = self.args[index+1] ramp = int(float(ramp) * 1000) dest = self.args[index-1] value = self.origin delta = dest - value delta = float(delta) step = delta / ramp print('ramp : ' + str(ramp), 'dest : ' + str(dest), 'origin : ' + str(value), 'step : ' + str(step)) for millisec in range(ramp): value += step sleep(0.00072) print(value) print('ramp ends in ' + self.name + ' at ' + str(datetime.datetime.now())) val = Ramp(1.0, [0.0, 'ramp', 1])
Hors ligne
Merci @avilleret pour ton aide hors-forum.
Voici la réponse à ma question…
#! /usr/bin/env python # -*- coding: utf-8 -*- import threading import time import datetime current_milli_time = lambda: time.time() * 1000 class Ramp(threading.Thread): """Instanciate a thread for Playing a ramp Allow to do several ramps in a same scenario""" def __init__(self, origin, args): threading.Thread.__init__(self) self.args = args self.origin = origin self.start() def run(self): print('ramp starts in ' + self.name + ' at ' + str(datetime.datetime.now())) index = self.args.index('ramp') ramp = self.args[index+1] ramp = int(float(ramp) * 1000) dest = self.args[index-1] value = self.origin index = self.args.index('grain') grain = float(self.args[index+1]) step = float( (dest - value) / ( float(ramp / grain) )) print('ramp : ' + str(ramp), 'dest : ' + str(dest), 'origin : ' + str(value), 'step : ' + str(step) + ' grain: '+ str(grain)) start = current_milli_time() last = start print(value) # print the first ValueError(" error") while (current_milli_time() < (start + ramp)): while (current_milli_time() < last + grain): pass # wait last = current_milli_time() value += step print(value) print('ramp ends in ' + self.name + ' at ' + str(datetime.datetime.now())) val = Ramp(1.0, [0.0, 'ramp', 1, 'grain', 50])
Dernière modification par reno- (2017-01-17 13:28:55)
Hors ligne
Pages: 1