ServoStik setting using runcommand-onstart.sh
-
I am building a two player Retropie 4.7.1 cabinet with two Ultimarc ServoStiks that should allow automatic switching between 4 way and 8 way. I had originally intended to set this up with two buttons on the cabinet to manually switch between the two modes, but ideally I’d like to automate it. I have downloaded the SetServoStik command line tool from RGBCommander. With that I can switch between the two modes using “SetServoStik 8/4”, works perfectly.
I have a file listing the configurations that I pulled together with my romlist and a spreadsheet of control configurations that want to apply for each arcade rom.
All of the previous threads on this don’t seem to have reached a conclusion.
The configurations consist of a file (servostik.cfg) that lists the rom name and a 4 or 8 for the configuration that I want. Like this:
1941 8 airwolf 8 altbeast 8 pacman 4 dkong 4 btime 4
I’m a bash noob, but did some research and can extract the 4 or 8 from my list of rom configs using grep and awk, but I’m struggling to get it working in a shell script. (I’m thinking I need to put it in runcommand-onstart.sh)
Using grep and awk from the command line I have managed to get the line/number that I want, something like this:
grep -w 'pacman' ./servostik.cfg | awk '{print $2}'
Where I'm struggling is then using that in the runcommand-onstart.sh
I can extract the rom name and strip the path / .zip bit, but then I'm struggling to construct my grep command, get its output and call the SetServoStik tool. I think my problem is with quotes, curly brackets and directory notation. As I said, I'm a unix shell scripting noob.
I could post my attempt at runcommand-onstart.sh if it would help?
Is runcommand-onstart.sh the right place to put this?
Where would be the best place to store the servostik.cfg file (I was thinking in a 'ServoStik' folder under the {system} roms folder?
-
@paul_uk said in ServoStik setting using runcommand-onstart.sh:
I could post my attempt at runcommand-onstart.sh if it would help?
Yes.
Is runcommand-onstart.sh the right place to put this?
Yes
Where would be the best place to store the servostik.cfg file (I was thinking in a 'ServoStik' folder under the {system} roms folder?
It doesn't matter, as long as it's accessible by the
onstart
script. Theroms
folder is accessible via file shares, so it's easy to edit it from a neighbouring PC without resorting to SSH. -
Thanks very much for the quick reply, I really appreciate it.
Prepare for a good laugh ;-)
I have taken a bit of a scattergun approach with quotes/curly brackets, reading online tutorials there seems to be a range of standards.
The config file is in (for example) 'roms/arcade/servostik"
The SetServoStik executable is in '~/RetroPie/ServoStik'
The "|| "abc 8" after the grep is my attempt at some error trapping so that if the cfg file isn't there or the room isn't found, it will default to 8.
I also wonder whether I should be doing something to track the current state of the ServoStik so that I'm not issuing unnecessary commands (each one drives the motor)
# /opt/retropie/configs/all/runcommand-onstart.sh # get the full path filename of the ROM rom=$3 # rom_bn receives $rom excluding everything from the first char to the last slash '/' rom_bn="${rom##*/}" # rom_bn receives $rom_bn excluding everything from the last char to the first dot '.' rom_bn="${rom_bn%.*}" # get the system name system=$1 # get the servostik config, from the file in the relevant system stikconfig="{grep -w "${rom_bn}" "~/RetroPie/roms/${system}/servostik/servostik.cfg" || "abc 8"}| awk {print $2}" #run the SetServoStik executable to set the sticks sudo ~/RetroPie/ServoStik/SetServoStik "${stikconfig}"
-
I have had another attempt to tidy this up and think that I am getting somewhere.
# /opt/retropie/configs/all/runcommand-onstart.sh # get the full path filename of the ROM rom=$3 # rom_bn receives $rom excluding everything from the first char to the last slash '/' rom_bn="${rom##*/}" # rom_bn receives $rom_bn excluding everything from the last char to the first dot '.' rom_bn="${rom_bn%.*}" # get the system name system=$1 # get the servostik config, from the file in the relevant system stikconfig="$(grep -w "${rom_bn}" "/home/pi/RetroPie/roms/${system}/servostik/servostik.cfg" | awk {'print $2'})" #if the config is anything other than "4" (due to an error) then set it to 8 if [[ $stikconfig != "4" ]]; then stikconfig="8" fi #run the SetServoStik executable to set the sticks (remove echo to run the command) echo "sudo /home/pi/RetroPie/ServoStik/SetServoStik "${stikconfig}""
So this seems to work! The only thing is that if the servostik.cfg is missing it echos the error message, I presume that isn't a problem?
Any other suggestions for improvement?
-
@paul_uk Looks fine. The last command shouldn't not be
echo
, but you probably added that just for checking what would be executed. -
@mitu said in ServoStik setting using runcommand-onstart.sh:
@paul_uk Looks fine. The last command shouldn't not be
echo
, but you probably added that just for checking what would be executed.Thanks, yes the echo was just for testing. Took a lot of fiddling to work out where spaces could and couldn’t be, but works now. I may do something to store the current state of the ServoStik so that it can save the motor when it is in the correct state.
-
So I have developed it a bit more so that it saves the state in text file. This means that it doesn't try and move the gate if it is already in the correct position. At first, the setservostik stopped working, I just couldn't work it out. Tested it on my PC and the board worked fine. Ended up downloading the setservostik source and rebuilding it as the github version seemed newer than the executable that I downloaded from RGBCommander. Sure enough, that gave me some more diagnostics that it did not have the privileges to write to USB. I had removed the 'sudo' as I didn't think it was necessary, turns out it was!
All working perfectly now. Here is my final version of the script for posterity in case anyone else fancies doing the same. Thanks for the moral support and guidance mitu.# /opt/retropie/configs/all/runcommand-onstart.sh # get the full path filename of the ROM rom=$3 # rom_bn receives $rom excluding everything from the first char to the last slash '/' rom_bn="${rom##*/}" # rom_bn receives $rom_bn excluding everything from the last char to the first dot '.' rom_bn="${rom_bn%.*}" # get the system name system=$1 # get the servostik config for this game, from the file in the relevant system stikconfig="$(grep -w "${rom_bn}" "/home/pi/RetroPie/roms/${system}/servostik/servostik.cfg" | awk {'print $2'})" #if the config is anything other than "4" (due to an error) then set it to 8 if [[ $stikconfig != "4" ]]; then stikconfig="8" fi #get the last known servostik config read ss < "/home/pi/RetroPie/roms/ss.txt" #run the SetServoStik executable to set the sticks, has to be run as super user to have USB access. setservostik source can be downloaded from https://github.com/mahuti/setservostik if [[ $ss != $stikconfig ]]; then sudo setservostik "${stikconfig}" echo $stikconfig > "/home/pi/RetroPie/roms/ss.txt" fi
-
@paul_uk Awesome thanks for this. I was using something from Katie Snow long ago but this seems a bit easier.
Contributions to the project are always appreciated, so if you would like to support us with a donation you can do so here.
Hosting provided by Mythic-Beasts. See the Hosting Information page for more information.