---
# System prepended metadata

title: RCbenchmark 自動控制程式

---

```
var test = [[1100, 4], [1200, 4], [1100, 4], [1000, 2]]; 
var time =0.025;
var ave=1;
// Test repeat
var repeat = 1; // set to 1 to run the sequence only once

// ESC initialization
var minVal = 1000;  // ESC initialization value (us)
var initDur = 4; // ESC initialization time (s)

var filePrefix = "CustomSequence"; 

///////////////// Beginning of the script //////////////////

// ESC initialization
rcb.console.print("Initializing ESC...");
rcb.output.set("esc",minVal);
rcb.wait(sequence, initDur);

// hide console debug info
rcb.console.setVerbose(false);

// start new log file
rcb.files.newLogFile({prefix: filePrefix});

// runs the sequence
var index = -1;
var total = repeat;
var totalTime = 0;
var steptimesum=0;
//var utcTime = new Date().getTime();



function sequence(){
    if(index > -1){
        if(index === test.length){
            // end of sequence
            index = -1;
            sequence();
        }else{
            var elapsed=0;
            var stepduration = test[index][1];
            // get step info
            var pwm = test[index][0];
             index ++;
            
            rcb.console.print("set esc time" +totalTime + " , " + pwm + " for " + stepduration + "s...");
            
            // set the output
            rcb.output.set("esc",pwm);
            
            var recordStep=function (){
                if(elapsed >= stepduration){
                    steptimesum+=stepduration;
                    totalTime=steptimesum;
                    sequence();
                }else{
                    var remaining = stepduration - elapsed;
                    //var waitTime = Math.min(time, remaining);
                    var startTimestamp = new Date().getTime();
                    
                    
                    // take a sample
                    
                    rcb.sensors.read(function(data){
                        data.time.displayValue = totalTime;
                        rcb.files.newLogEntry(data);
                        //rcb.console.print("sample saved");
                    }, ave);
                    
                    var endTimestamp = new Date().getTime();
                    var functiontime=endTimestamp-startTimestamp;
                    elapsed += time;
                    totalTime +=time;
                    rcb.console.print("sample time" +totalTime+" function time"+functiontime);
                    if(time < (functiontime/1000)*1.1){
                        rcb.output.ramp("esc", pwm, minVal, 5 );
                        rcb.console.print("sample time < function time, sample time has to bigger than " +functiontime/1000+ " ms" );
                        rcb.wait(5);
                        rcb.output.set("esc",minVal);
                        rcb.endScript();
                    }
                    rcb.wait(recordStep,Math.max(0,time-functiontime/1000));
                    }
                };

            recordStep();
        }
    }else{
        if(repeat > 0){
            rcb.console.print("Sequence " + (total-repeat+1) + " of " + total);
            repeat--;
            index++;
            sequence();
        }else{
            rcb.console.print("end time" +totalTime);
            rcb.endScript();
            }
        }
}
```