当前位置: 首页>>代码示例>>Python>>正文


Python Recorder.record方法代码示例

本文整理汇总了Python中recorder.Recorder.record方法的典型用法代码示例。如果您正苦于以下问题:Python Recorder.record方法的具体用法?Python Recorder.record怎么用?Python Recorder.record使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在recorder.Recorder的用法示例。


在下文中一共展示了Recorder.record方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_record_when_title_and_command_given

# 需要导入模块: from recorder import Recorder [as 别名]
# 或者: from recorder.Recorder import record [as 别名]
    def test_record_when_title_and_command_given(self):
        recorder = Recorder(self.pty_recorder)

        asciicast = recorder.record('ls -l', 'the title')

        assert_equal('the title', asciicast.title)
        assert_equal('ls -l', asciicast.command)
        assert_equal(('ls -l',), self.pty_recorder.record_call_args())
        assert_equal(123.45, asciicast.duration)
        assert_equal(self.pty_recorder.stdout, asciicast.stdout)
开发者ID:caseyscarborough,项目名称:asciinema,代码行数:12,代码来源:recorder_test.py

示例2: test_record_when_no_title_nor_command_given

# 需要导入模块: from recorder import Recorder [as 别名]
# 或者: from recorder.Recorder import record [as 别名]
    def test_record_when_no_title_nor_command_given(self):
        env = { 'SHELL': '/bin/blush' }
        recorder = Recorder(self.pty_recorder, env)

        asciicast = recorder.record(None, None)

        assert_equal(None, asciicast.title)
        assert_equal(None, asciicast.command)
        assert_equal(('/bin/blush',), self.pty_recorder.record_call_args())
        assert_equal(123.45, asciicast.duration)
        assert_equal(self.pty_recorder.stdout, asciicast.stdout)
开发者ID:caseyscarborough,项目名称:asciinema,代码行数:13,代码来源:recorder_test.py

示例3: Control

# 需要导入模块: from recorder import Recorder [as 别名]
# 或者: from recorder.Recorder import record [as 别名]
class Control(control.Control):
    """
    A controller that implements operational space control.
    Controls the (x,y) position of a robotic arm end-effector.
    """
    def __init__(self, null_control=True, **kwargs): 
        """
        null_control boolean: apply second controller in null space or not
        """

        super(Control, self).__init__(**kwargs)

        self.DOF = 2 # task space dimensionality 
        self.null_control = null_control

        if self.write_to_file is True:
            from recorder import Recorder
            # set up recorders
            self.u_recorder = Recorder('control signal', self.task, 'osc')
            self.xy_recorder = Recorder('end-effector position', self.task, 'osc')
            self.dist_recorder = Recorder('distance from target', self.task, 'osc')
            self.recorders = [self.u_recorder, 
                            self.xy_recorder, 
                            self.dist_recorder]

    def control(self, arm, x_des=None):
        """Generates a control signal to move the 
        arm to the specified target.
            
        arm Arm: the arm model being controlled
        des list: the desired system position
        x_des np.array: desired task-space force, 
                        system goes to self.target if None
        """

        # calculate desired end-effector acceleration
        if x_des is None:
            self.x = arm.x 
            x_des = self.kp * (self.target - self.x)

        # generate the mass matrix in end-effector space
        Mq = arm.gen_Mq()
        Mx = arm.gen_Mx()

        # calculate force 
        Fx = np.dot(Mx, x_des)

        # calculate the Jacobian 
        JEE = arm.gen_jacEE()
        # tau = J^T * Fx + tau_grav, but gravity = 0
        # add in velocity compensation in GC space for stability
        self.u = np.dot(JEE.T, Fx).reshape(-1,) - np.dot(Mq, self.kv * arm.dq)

        # if null_control is selected and the task space has 
        # fewer DOFs than the arm, add a control signal in the 
        # null space to try to move the arm to its resting state
        if self.null_control and self.DOF < len(arm.L): 

            # calculate our secondary control signal
            # calculated desired joint angle acceleration
            prop_val = ((arm.rest_angles - arm.q) + np.pi) % (np.pi*2) - np.pi
            q_des = (self.kp * prop_val + \
                     self.kv * -arm.dq).reshape(-1,)

            Mq = arm.gen_Mq()
            u_null = np.dot(Mq, q_des)

            # calculate the null space filter
            Jdyn_inv = np.dot(Mx, np.dot(JEE, np.linalg.inv(Mq)))
            null_filter = np.eye(len(arm.L)) - np.dot(JEE.T, Jdyn_inv)

            null_signal = np.dot(null_filter, u_null).reshape(-1,)

            self.u += null_signal

        if self.write_to_file is True:
            # feed recorders their signals
            self.u_recorder.record(0.0, self.u)
            self.xy_recorder.record(0.0, self.x)
            self.dist_recorder.record(0.0, self.target - self.x)
 
        # add in any additional signals 
        for addition in self.additions:
            self.u += addition.generate(self.u, arm)
           
        return self.u

    def gen_target(self, arm):
        """Generate a random target"""
        gain = np.sum(arm.L) * .75
        bias = -np.sum(arm.L) * 0
        
        self.target = np.random.random(size=(2,)) * gain + bias

        return self.target.tolist()
开发者ID:Chunting,项目名称:control,代码行数:97,代码来源:osc.py

示例4: Control

# 需要导入模块: from recorder import Recorder [as 别名]
# 或者: from recorder.Recorder import record [as 别名]
class Control(control.Control):
    """
    A controller that loads in a neural network trained using the
    hessianfree (https://github.com/drasmuss/hessianfree) library 
    to control a simulated arm.
    """
    def __init__(self, **kwargs): 

        super(Control, self).__init__(**kwargs)

        self.old_target = [None, None]

        # load up our network
        import glob

        # this code goes into the weights folder, finds the most 
        # recent trial, and loads up the weights
        files = sorted(glob.glob('controllers/weights/rnn*'))
        print 'loading weights from %s'%files[-1]
        W = np.load(files[-1])['arr_0']
        num_states = 4
        self.rnn = RNNet(shape=[num_states * 2, 32, 32, num_states, num_states], 
                     layers=[Linear(), Tanh(), Tanh(), Linear(), Linear()],
                     rec_layers=[1,2],
                     conns={0:[1, 2], 1:[2], 2:[3], 3:[4]},
                     load_weights=W,
                     use_GPU=False)

        offset, W_end, b_end = self.rnn.offsets[(3,4)]
        self.rnn.mask = np.zeros(self.rnn.W.shape, dtype=bool)
        self.rnn.mask[offset:b_end] = True
        self.rnn.W[offset:W_end] = np.eye(4).flatten()

        self.joint_targets = None
        self.act = None

        # set up recorders
        if self.write_to_file is True:
            from recorder import Recorder
            self.u_recorder = Recorder('control signal', self.task, 'hf')
            self.xy_recorder = Recorder('end-effector position', self.task, 'hf')
            self.dist_recorder = Recorder('distance from target', self.task, 'hf')
            self.recorders = [self.u_recorder, 
                            self.xy_recorder, 
                            self.dist_recorder]

    def control(self, arm, x_des=None):
        """Generates a control signal to move the 
        arm to the specified target.
            
        arm Arm: the arm model being controlled
        des list: the desired system position
        x_des np.array: desired task-space force, 
                        system goes to self.target if None
        """

        self.x = arm.x

        # if the target has changed, convert into joint angles again
        if np.any(self.old_target != self.target):
            self.joint_targets = arm.inv_kinematics(xy=self.target)
            self.old_target = self.target
            
        inputs = np.concatenate([self.joint_targets, np.zeros(2), arm.q, arm.dq])[None,None,:]
        self.act = [a[:,-1,:] for a in self.rnn.forward(inputs, init_activations=self.act)]
        u = self.act[-1][0]
        # NOTE: Make sure this is set up the same way as in training
        # use all the network output is the control signal
        self.u = np.array([np.sum(u[ii::arm.DOF]) for ii in range(arm.DOF)])

        if self.write_to_file is True:
            # feed recorders their signals
            self.u_recorder.record(0.0, self.u)
            self.xy_recorder.record(0.0, self.x)
            self.dist_recorder.record(0.0, self.target - self.x)

        # add in any additional signals 
        for addition in self.additions:
            self.u += addition.generate(self.u, arm)

        return self.u

    def gen_target(self, arm):
        pass
开发者ID:himlen1990,项目名称:control,代码行数:86,代码来源:ahf.py

示例5: __init__

# 需要导入模块: from recorder import Recorder [as 别名]
# 或者: from recorder.Recorder import record [as 别名]
class SpeechToSpeech:
    def __init__(self, config_path):
        # read config file
        config = ConfigParser.RawConfigParser()
        config.read(config_path)

        # general
        self.tmp_file = config.get('general', 'tmp_file')

        # gsr api
        self.gsr_api_lang = config.get('gsr_api', 'language')

        # moses
        self.e = config.get('moses', 'e')
        self.f = config.get('moses', 'f')
        self.moses_config_orig = config.get('moses', 'config_orig')
        self.moses_config_fix = config.get('moses', 'config_fix')
        self.moses_dir = config.get('moses', 'moses_dir')
        self.model_dir = config.get('moses', 'model_dir')
        self.model_dir_jenkins = config.get('moses', 'model_dir_jenkins')
        self.threads = config.getint('moses', 'threads')

        # pyttsx
        self.tts_voice_id = config.get('pyttsx', 'voice')
        self.tts_voice_rate = config.getint('pyttsx', 'rate')

        # create recorder
        self.recorder = Recorder(language=self.gsr_api_lang)

    @staticmethod
    def replace_lines(in_file, out_file, from_line, to_line):
        with open(out_file, 'wt') as cfg_fix:
            with open(in_file, 'rt') as cfg_orig:
                for line in cfg_orig:
                    cfg_fix.write(line.replace(from_line, to_line))

    def fix_moses_config(self):
        # fix moses config
        cfg_fix = self.model_dir + self.moses_config_fix
        cfg_orig = self.model_dir + self.moses_config_orig
        line_orig = self.model_dir_jenkins
        line_fix = self.model_dir
        self.replace_lines(cfg_orig, cfg_fix, line_orig, line_fix)

    def moses_translation(self, stt_file):
        # use shell for translation
        shell_cmd = 'cat {5} |' \
                    '{0}scripts/tokenizer/tokenizer.perl -threads {2} -l {3} |' \
                    '{0}bin/moses -threads {2} -f {1}moses-sts.ini |' \
                    '{0}scripts/tokenizer/detokenizer.perl -threads {2} -l {4}'.format(self.moses_dir,
                                                                                       self.model_dir,
                                                                                       self.threads,
                                                                                       self.f,
                                                                                       self.e,
                                                                                       stt_file)

        # run translation
        moses_process = subprocess.Popen(shell_cmd, shell=True, stdout=subprocess.PIPE)
        return moses_process.stdout.read()

    def record_sample(self):
        flac_file = self.recorder.record()
        file_name, _ = os.path.splitext(flac_file)
        stt_file = self.recorder.speech_to_text(flac_file)

        return stt_file

    def translate(self, stt_file):
        if os.path.isfile(stt_file) and os.path.getsize(stt_file) > 0:
            file_name, _ = os.path.splitext(stt_file)

            # fix moses config file downloaded from jenkins
            self.fix_moses_config()

            translation = self.moses_translation(stt_file)

            # save translation
            trans_file_name = file_name + '-translated.txt'
            with open(trans_file_name, 'wt') as trans_file:
                trans_file.write(translation)

            print '* saved translation in', trans_file_name

            return translation.decode('utf8')

    def translate_file(self, file):
        flac_file = self.recorder.convert_to_flac(file)
        stt_file = self.recorder.speech_to_text(flac_file)

        return self.translate(stt_file)

    def translate_text(self, text):
        with open(self.tmp_file, 'wb') as tmp_file:
            tmp_file.write(text)

        return self.translate(self.tmp_file)

    def tts(self, text):
        print '* tts:', text
        engine = pyttsx.init()
#.........这里部分代码省略.........
开发者ID:katzoo,项目名称:amu,代码行数:103,代码来源:sts-moses.py

示例6: ArgumentParser

# 需要导入模块: from recorder import Recorder [as 别名]
# 或者: from recorder.Recorder import record [as 别名]
    parser = ArgumentParser(description="""\
Copy n seconds from source video to destination.""",
                           )
    parser.add_argument('-s', '--seconds', dest='seconds', type=int, default=5)
    parser.add_argument(nargs=1, dest='in_file', type=str)
    parser.add_argument(nargs=1, dest='out_file', type=str)
    args = parser.parse_args()
    
    args.in_file = path(args.in_file[0])
    args.out_file = path(args.out_file[0])
    if args.in_file.abspath() == args.out_file.abspath():
        raise ValueError, 'Input path and output path must be different.'
    
    return args


if __name__ == '__main__':
    from safe_cv import cv

    args = parse_args()
    cap_config = CVCaptureConfig(args.in_file, type_='file')
    r = Recorder(cap_config, args.out_file)

    print 'start recording'
    r.record()

    sleep(args.seconds)

    print 'stop recording'
    r.stop()
开发者ID:cfobel,项目名称:opencv_helpers,代码行数:32,代码来源:recorder_test.py

示例7: raw_input

# 需要导入模块: from recorder import Recorder [as 别名]
# 或者: from recorder.Recorder import record [as 别名]
# Standard Modules
import sys
import os
# Modules from the GitHub
from wit import Wit
# The chunks
from recorder import Recorder
from interpreter import Interpreter

# Constants
SECONDS = 4

if __name__ == '__main__':

    # Set the Wit.AI token from an environment variable
    if 'WIT_TOKEN' not in os.environ:
        os.environ['WIT_TOKEN'] = raw_input("Enter your Wit.AI token: ")
    witToken = os.environ['WIT_TOKEN']

    # Instantiate the chunks
    aRecording = Recorder(SECONDS)
    anInterpreting = Interpreter()
    witty = Wit(witToken)

    # Run with it
    audio_file = aRecording.record()
    result = witty.post_speech(audio_file.getvalue())
    anInterpreting.interpret(result)

    # And we're done
    sys.exit(0)
开发者ID:Legomaniac,项目名称:sonos_wit,代码行数:33,代码来源:sw.py

示例8: Control

# 需要导入模块: from recorder import Recorder [as 别名]
# 或者: from recorder.Recorder import record [as 别名]
class Control(control.Control):
    """
    A controller that implements operational space control.
    Controls the (x,y) position of a robotic arm end-effector.
    """
    def __init__(self, solve_continuous=False, **kwargs): 

        super(Control, self).__init__(**kwargs)

        self.DOF = 2 # task space dimensionality 
        self.u = None
        self.solve_continuous = solve_continuous

        if self.write_to_file is True:
            from recorder import Recorder
            # set up recorders
            self.u_recorder = Recorder('control signal', self.task, 'lqr')
            self.xy_recorder = Recorder('end-effector position', self.task, 'lqr')
            self.dist_recorder = Recorder('distance from target', self.task, 'lqr')
            self.recorders = [self.u_recorder, 
                            self.xy_recorder, 
                            self.dist_recorder]

    def calc_derivs(self, x, u):
        eps = 0.00001  # finite difference epsilon
        #----------- compute xdot_x and xdot_u using finite differences --------
        # NOTE: here each different run is in its own column
        x1 = np.tile(x, (self.arm.DOF*2,1)).T + np.eye(self.arm.DOF*2) * eps
        x2 = np.tile(x, (self.arm.DOF*2,1)).T - np.eye(self.arm.DOF*2) * eps
        uu = np.tile(u, (self.arm.DOF*2,1))
        f1 = self.plant_dynamics(x1, uu)
        f2 = self.plant_dynamics(x2, uu)
        xdot_x = (f1 - f2) / 2 / eps
   
        xx = np.tile(x, (self.arm.DOF,1)).T 
        u1 = np.tile(u, (self.arm.DOF,1)) + np.eye(self.arm.DOF) * eps
        u2 = np.tile(u, (self.arm.DOF,1)) - np.eye(self.arm.DOF) * eps
        f1 = self.plant_dynamics(xx, u1)
        f2 = self.plant_dynamics(xx, u2)
        xdot_u = (f1 - f2) / 2 / eps

        return xdot_x, xdot_u

    def control(self, arm, x_des=None):
        """Generates a control signal to move the 
        arm to the specified target.
            
        arm Arm: the arm model being controlled
        des list: the desired system position
        x_des np.array: desired task-space force, 
                        system goes to self.target if None
        """
        if self.u is None:
            self.u = np.zeros(arm.DOF)

        self.Q = np.zeros((arm.DOF*2, arm.DOF*2))
        self.Q[:arm.DOF, :arm.DOF] = np.eye(arm.DOF) * 1000.0 
        self.R = np.eye(arm.DOF) * 0.001 

        # calculate desired end-effector acceleration
        if x_des is None:
            self.x = arm.x 
            x_des = self.x - self.target 

        self.arm, state = self.copy_arm(arm)
        A, B = self.calc_derivs(state, self.u)

        if self.solve_continuous is True:
            X = sp_linalg.solve_continuous_are(A, B, self.Q, self.R)
            K = np.dot(np.linalg.pinv(self.R), np.dot(B.T, X))
        else: 
            X = sp_linalg.solve_discrete_are(A, B, self.Q, self.R)
            K = np.dot(np.linalg.pinv(self.R + np.dot(B.T, np.dot(X, B))), np.dot(B.T, np.dot(X, A)))

        # transform the command from end-effector space to joint space
        J = self.arm.gen_jacEE()

        u = np.hstack([np.dot(J.T, x_des), arm.dq])

        self.u = -np.dot(K, u)

        if self.write_to_file is True:
            # feed recorders their signals
            self.u_recorder.record(0.0, self.u)
            self.xy_recorder.record(0.0, self.x)
            self.dist_recorder.record(0.0, self.target - self.x)

        # add in any additional signals 
        for addition in self.additions:
            self.u += addition.generate(self.u, arm)

        return self.u
 
    def copy_arm(self, real_arm):
        """ Make a copy of the arm for local simulation. """
        arm = real_arm.__class__()
        arm.dt = real_arm.dt

        # reset arm position to x_0
        arm.reset(q = real_arm.q, dq = real_arm.dq)
#.........这里部分代码省略.........
开发者ID:Chunting,项目名称:control,代码行数:103,代码来源:lqr.py

示例9: __init__

# 需要导入模块: from recorder import Recorder [as 别名]
# 或者: from recorder.Recorder import record [as 别名]
class Dashcam:
    LOG_FILE = "/home/pi/dashcam_v2/dashcam.log"
    MINIMUM_FREE_SPACE = 500  # 500 MB
    JAN_1ST_2016 = 1451624400

    def __init__(self):
        logging.basicConfig(
            level=logging.INFO,
            filename=Dashcam.LOG_FILE,
            format="%(asctime)-15s %(name)-12s %(levelname)s %(message)s",
            datefmt="%y-%m-%d %H:%M:%S",
        )
        self.logger = logging.getLogger(self.__class__.__name__)
        current_time = time.localtime()
        if current_time < Dashcam.JAN_1ST_2016:
            # we probably doesnt have RTC, do not raise error for now
            self.logger.error("No RTC? Current time is " + str(datetime.datetime.now()))
            # raise ValueError("There doesn't seem to be a RTC")
        self.recorder = Recorder()

    def start(self):
        self.logger.info("Starting recorder")
        while True:
            free_MB = self.get_free_MB()
            if free_MB < Dashcam.MINIMUM_FREE_SPACE:
                self.logger.error(
                    "Not enough free space! requried: {r}, have: {h}".format(r=Dashcam.MINIMUM_FREE_SPACE, h=free_MB)
                )
                while self.get_free_MB() < Dashcam.MINIMUM_FREE_SPACE:
                    self.remove_oldest_file()
            # record for 10 minutes (on low resolution)
            timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
            self.recorder.record(timestamp, duration=600, low_resolution=False)

    def remove_oldest_file(self, extension=".h264"):
        try:
            oldest = min(
                (
                    os.path.join(dirname, filename)
                    for dirname, _, filenames in os.walk(Recorder.RECORDING_DIR)
                    for filename in filenames
                    if filename.endswith(extension)
                ),
                key=lambda fn: os.stat(fn).st_mtime,
            )
        except ValueError:
            self.logger.error(
                "Could not find any file in {d} that ends with {ext}".format(d=Recorder.RECORDING_DIR, ext=extension)
            )
            return
        self.logger.warning("Removing {f}".format(f=oldest))
        os.remove(oldest)

    def get_free_MB(self, path="/"):
        """Return disk usage statistics about the given path.
        """
        st = os.statvfs(path)
        free = st.f_bavail * st.f_frsize
        total = st.f_blocks * st.f_frsize
        used = (st.f_blocks - st.f_bfree) * st.f_frsize
        self.logger.debug("Disk stats: free: {f}B, used: {u}B, total: {t}B".format(f=free, u=used, t=total))
        # return free space in MB
        return free / 1000000
开发者ID:yimingkang,项目名称:dashcam_v2,代码行数:65,代码来源:main.py

示例10: TestRecorder

# 需要导入模块: from recorder import Recorder [as 别名]
# 或者: from recorder.Recorder import record [as 别名]
class TestRecorder(unittest.TestCase):
    def setUp(self):
        self.outputdir = tempfile.mkdtemp()
        self.inputdir = tempfile.mkdtemp()
        self.tmpimages = []
        self.recorder = Recorder(self.inputdir, self.outputdir)

    def create_temp_image(self, name, dimens, color):
        im = Image.new("RGBA", dimens, color)
        filename = os.path.join(self.inputdir, name)
        im.save(filename, "PNG")
        return filename

    def make_metadata(self, str):
        with open(os.path.join(self.inputdir, "metadata.xml"), "w") as f:
            f.write(str)

    def tearDown(self):
        for f in self.tmpimages:
            f.close()

        shutil.rmtree(self.outputdir)
        shutil.rmtree(self.inputdir)

    def test_create_temp_image(self):
        im = self.create_temp_image("foobar", (100, 10), "blue")
        self.assertTrue(os.path.exists(im))

    def test_recorder_creates_dir(self):
        shutil.rmtree(self.outputdir)
        self.make_metadata("""<screenshots></screenshots>""")
        self.recorder.record()

        self.assertTrue(os.path.exists(self.outputdir))

    def test_single_input(self):
        self.create_temp_image("foobar.png", (10, 10), "blue")
        self.make_metadata("""<screenshots>
<screenshot>
   <name>foobar</name>
   <tile_width>1</tile_width>
   <tile_height>1</tile_height>
</screenshot>
</screenshots>""")

        self.recorder.record()
        self.assertTrue(exists(join(self.outputdir, "foobar.png")))

    def test_two_files(self):
        self.create_temp_image("foo.png", (10, 10), "blue")
        self.create_temp_image("bar.png", (10, 10), "red")
        self.make_metadata("""<screenshots>
<screenshot>
   <name>foo</name>
   <tile_width>1</tile_width>
   <tile_height>1</tile_height>
</screenshot>
<screenshot>
   <name>bar</name>
   <tile_width>1</tile_width>
   <tile_height>1</tile_height>
</screenshot>
</screenshots>""")

        self.recorder.record()
        self.assertTrue(exists(join(self.outputdir, "foo.png")))
        self.assertTrue(exists(join(self.outputdir, "bar.png")))

    def test_one_col_tiles(self):
        self.create_temp_image("foobar.png", (10, 10), "blue")
        self.create_temp_image("foobar_0_1.png", (10, 10), "red")

        self.make_metadata("""<screenshots>
<screenshot>
   <name>foobar</name>
    <tile_width>1</tile_width>
    <tile_height>2</tile_height>
</screenshot>
</screenshots>""")

        self.recorder.record()

        im = Image.open(join(self.outputdir, "foobar.png"))
        (w, h) = im.size
        self.assertEquals(10, w)
        self.assertEquals(20, h)

        self.assertEquals((0, 0, 255, 255), im.getpixel((1, 1)))
        self.assertEquals((255, 0, 0, 255), im.getpixel((1, 11)))

    def test_one_row_tiles(self):
        self.create_temp_image("foobar.png", (10, 10), "blue")
        self.create_temp_image("foobar_1_0.png", (10, 10), "red")

        self.make_metadata("""<screenshots>
<screenshot>
   <name>foobar</name>
    <tile_width>2</tile_width>
    <tile_height>1</tile_height>
</screenshot>
#.........这里部分代码省略.........
开发者ID:RkShaRkz,项目名称:screenshot-tests-for-android,代码行数:103,代码来源:test_recorder.py

示例11: Control

# 需要导入模块: from recorder import Recorder [as 别名]
# 或者: from recorder.Recorder import record [as 别名]
class Control(control.Control):
    """
    A controller that implements operational space control.
    Controls the (x,y) position of a robotic arm end-effector.
    """
    def __init__(self, **kwargs): 

        super(Control, self).__init__(**kwargs)

        self.DOF = 2 # task space dimensionality 
        self.u = None

        # specify which gradient approximation method to use
        self.gradient_approximation = self.spsa

        if self.write_to_file is True:
            from recorder import Recorder
            # set up recorders
            self.xy_recorder = Recorder('end-effector position', self.task, 'gradient_spsa')
            self.recorders = [self.xy_recorder]

    def fdsa(self, x, u, ck):
        """ Estimate the gradient of a self.cost using 
        Finite Differences Stochastic Approximation (FDSA). 

        x np.array: state of the function
        u np.array: control signal
        ck: magnitude of perturbation
        """
        gk = np.zeros(u.shape)
        for ii in range(gk.shape[0]):
            # Generate perturbations one parameter at a time
            inc_u = np.copy(u)
            inc_u[ii] += ck
            dec_u = np.copy(u) 
            dec_u[ii] -= ck

            # Function evaluation
            cost_inc = self.cost(np.copy(x), inc_u)
            cost_dec = self.cost(np.copy(x), dec_u)

            # Gradient approximation
            gk[ii] = (cost_inc - cost_dec) / (2.0 * ck)
        return gk

    def spsa(self, x, u, ck):
        """ Estimate the gradient of a self.cost using 
        Simultaneous Perturbation Stochastic Approximation (SPSA). 
        Implemented base on (Spall, 1998).

        x np.array: state of the function
        u np.array: control signal
        ck: magnitude of perturbation
        """
        # Generate simultaneous perturbation vector.
        # Choose each component from a bernoulli +-1 distribution 
        # with probability of .5 for each +-1 outcome.
        delta_k = np.random.choice([-1,1], size=self.arm.DOF, 
                                   p=[.5, .5])

        # Function evaluations
        inc_u = np.copy(u) + ck * delta_k
        cost_inc = self.cost(np.copy(x), inc_u)
        dec_u = np.copy(u) - ck * delta_k
        cost_dec = self.cost(np.copy(x), dec_u)

        # Gradient approximation
        gk = np.dot((cost_inc - cost_dec) / (2.0*ck), delta_k)
        return gk

    def cost(self, x, u): 
        """ Calculate the cost of applying u in state x. """
        dt = .1 if self.arm.DOF == 3 else .01
        next_x = self.plant_dynamics(x, u, dt=dt)
        vel_gain = 100 if self.arm.DOF == 3 else 10
        return (np.sqrt(np.sum((self.arm.x - self.target)**2)) * 1000 \
                + np.sum((next_x[self.arm.DOF:])**2) * vel_gain)

    def control(self, arm, x_des=None):
        """ Use gradient approximation to calculate a 
        control signal that minimizes self.cost()

        arm Arm: the arm model being controlled
        x_des np.array: desired task-space force, 
                        system goes to self.target if None
        """  

        self.x = arm.x
        self.arm, state = self.copy_arm(arm)

        # Step 1: Initialization and coefficient selection
        max_iters = 10
        converge_thresh = 1e-5

        alpha = 0.602 # from (Spall, 1998)
        gamma = 0.101
        a = .101 # found empirically using HyperOpt
        A = .193
        c = .0277

#.........这里部分代码省略.........
开发者ID:himlen1990,项目名称:control,代码行数:103,代码来源:gradient_approximation.py


注:本文中的recorder.Recorder.record方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。