Skip to content

Instantly share code, notes, and snippets.

@GOROman
Last active January 3, 2022 15:04
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GOROman/91974e20ad1f458f2b95a9d3a391827e to your computer and use it in GitHub Desktop.
Save GOROman/91974e20ad1f458f2b95a9d3a391827e to your computer and use it in GitHub Desktop.
プリメイドAI (Premaid AI) のモーションフォーマット(.pma)を解析する
#!/usr/bin/env ruby -
# coding: utf-8
# Premaid AI - .pma file analyzer.
SERVO_OFFSET = 7500
class PMAFile
def initialize
@buf = ""
@data = []
end
def load( filename )
@buf = File.read( filename )
parse()
end
def parse()
if ( @buf =~ /=(\S\S\s.+)/ )
$1.split.each do |m|
@data << m.hex
end
end
end
def dump
i = 0
@data.size.times do |t|
len = @data[i]; i += 1
break if len == 0xff
xor = len
(len-2).times { |t| xor ^= @data[i+t] } # チェックバイト計算
cmd = @data[i]; i += 1
printf("---- Cmd[%02x] Len:%02x(%2d) ", cmd, len, len)
case cmd
when 0x18 then
@data[i]; i += 1
frame = @data[i]; i += 1
printf("\n [Pos] Frame:%3d\n", frame)
((len-4)/3).times {
servo_id = @data[i]; i+= 1
servo_angle = @data[i]; i+= 1
servo_angle += @data[i]<<8; i+= 1
printf(" Servo ID[%2d] Pos:%6d (%6d)\n", servo_id, servo_angle, servo_angle-SERVO_OFFSET)
}
when 0x19 then
printf("\n [ServoParam]\n")
((len-3)).times {
d = @data[i] ; i += 1
printf("%3d,", d)
}
else # UNKNOWN
(len-3).times do |b|
param = @data[i]; i += 1
printf("%02x ", param)
end
end
parity = @data[i]; i+= 1
printf("<Parity:%02x=%02x>", parity, xor)
if parity != xor
printf("Error: Parse error!\n")
return
end
puts
end
end
end
if ARGV.size < 1
STDERR.print "Usage: #{$0} [Premaid AI .pma file]...\n"
exit
else
ARGV.each do |filename|
printf("Filename[%s]\n", filename)
pma = PMAFile.new
pma.load( filename )
pma.dump
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment