Some automation to supplement Cleanflight-configurator

NAZE32acro

Work continues on the quadcopter using Cleanflight on a NAZE32 flight controller.

Command line interface – backup

Cleanflight-configurator is a great tool, but it is not comprehensive, and its backups do not include parameters not set by the GUI.

To supplement Cleanflight-configurator, I have written an expect script to take a backup using the command line interface.

#!/usr/bin/expect
package require Expect

encoding system iso8859-1

if {[lindex $argv 0] == ""} {
  set port {\\.\COM20}
#  set port /dev/ttyUSB0
} else {
  set port [lindex $argv 0]
}

set baud 115200
send_tty "Open port $port\n";
set com [open $port r+]
fconfigure $com -mode $baud,n,8,1 -encoding binary -buffering none -translation binary
set spawned [spawn -open $com]
set timeout 2
set newTime [clock seconds]
set dumpfile [clock format $newTime -format CfCliBu-%Y%m%d%H%M%S.txt]
set fh [open $dumpfile w]
after 1000;
send "#\n";
expect {
  "#" {send "rateprofile 0\n";}
  }
after 200;
expect {
  "rateprofile" {send "profile 0\n";}
  }
after 200;
expect {
  "profile" {send "dump\n";}
  }
expect "# dump\n";
expect {
  "\n" { puts -nonewline $fh "$expect_out(buffer)"; exp_continue}
  eof {}
  timeout {}
  }
close $fh
exit

Under Activestate TCL for Windows, expect scripts are run at the Windows command prompt by the command tclsh scriptname.expect. Under Linux, just make them executable and invoke them.

Command line interface – restore

Of course, that begs a method of restoring the backup, but given that Cleanflight does not really provide backwards compatibility, blind restoration of a backup from an earlier version is potentially dangerous. For that reason, I restore only changes from defaults which I manually accumulate in a file.

#!/usr/bin/expect
package require Expect

encoding system iso8859-1

if {[lindex $argv 0] == ""} {
  set port {\\.\COM20}
#  set port /dev/ttyUSB0
} else {
  set port [lindex $argv 0]
}
set timeout 5 ; #BT is slow to open
set baud 115200
send_tty "Open port $port\n";
set com [open $port r+]
fconfigure $com -mode $baud,n,8,1 -encoding binary -buffering none -translation binary
#fconfigure $com -mode $baud,n,8,1  -buffering none -translation binary
set spawned [spawn -open $com]
set infile [open "CfCliCmd.txt" r]
after 1000
send "#\n"
expect {
  "Rebooting" {}
  "Invalid" {}
  "#" {
    if { [gets $infile line] >= 0 } then {
      if [ regexp {^(#|$)} $line matchresult ] then {
        send_tty "~$line\n"; #echo skipped line
        send "#\n"; # cause another # prompt 
      } else {
        send "$line\n"
      }
    after 500 ;
    exp_continue
    }
  }
}
close $infile
exit

An example set of commands to apply:

set gyro_lpf = 42HZ
set blackbox_rate_num = 1
set blackbox_rate_denom = 2
save

Bulk rename of Cleanflight-configurator backups

The default backup naming convention in Cleanflight-configurator leaves much to be desired, so I have created a script to batch rename files using AdvancedRenamer.

@echo off
rem batch file to rename default filenames from cleanflight config backup
rem Owen Duffy 20160612

set BATDIR=%~dp0
      
SET ProgFiles86Root=%ProgramFiles(x86)%
IF NOT "%ProgFiles86Root%"=="" GOTO win64
SET ProgFiles86Root=%ProgramFiles%
:win64
set ARENC=Advanced Renamer\arenc
rem fileu -l -a -f c:\OpenSSL-Win32 "1 2" lib*2.dll

rem "%ProgFiles86Root%\%ARENC%" 

"%ProgFiles86Root%\%ARENC%" -e %BATDIR%CleanflightBackupRename.aren -p . -msk "cleanflight_backup_*.txt"

… and the CleanflightBackupRename.aren file contents:

[header]
type=preset
application=Advanced Renamer 3.72
application_version=3720000
batchmode=rename

[namecollision]
separator=_
pattern=
rule=fail

[methods]
method0000=methodname:"renumber"; active:"1"; numberposition:"1"; change:"relative"; newnumber:"0"; skip:"1"; zeropad:"manual"; numberlength:"2"; applyto:"name"; negativenumbers:"0";
method0001=methodname:"renumber"; active:"1"; numberposition:"2"; change:"relative"; newnumber:"0"; skip:"1"; zeropad:"manual"; numberlength:"2"; applyto:"name"; negativenumbers:"0";
method0002=methodname:"renumber"; active:"1"; numberposition:"3"; change:"relative"; newnumber:"0"; skip:"1"; zeropad:"manual"; numberlength:"4"; applyto:"name"; negativenumbers:"0";
method0003=methodname:"renumber"; active:"1"; numberposition:"4"; change:"relative"; newnumber:"0"; skip:"1"; zeropad:"manual"; numberlength:"2"; applyto:"name"; negativenumbers:"0";
method0004=methodname:"renumber"; active:"1"; numberposition:"5"; change:"relative"; newnumber:"0"; skip:"1"; zeropad:"manual"; numberlength:"2"; applyto:"name"; negativenumbers:"0";
method0005=methodname:"replace"; active:"1"; replace:"(\d+)\.(\d+)\.(\d+)\.(\d+)\.(\d+)"; replacewith:"\3\1\2\4\5"; casesensitive:"0"; regularexpressions:"1"; applyto:"name"; occurrence:"0";
method0006=methodname:"replace"; active:"1"; replace:"cleanflight_backup_"; replacewith:"Q21-"; casesensitive:"0"; regularexpressions:"0"; applyto:"name"; occurrence:"0";

Double clicking on CfBuBuRen.bat in Windows explorer will run the rename on all files with the default filename. The renamed files collate in date order.