#S1歌舞大赛#蝶恋+心形曲线走位+绚丽灯效
8
发表于2019-10-28
手机看帖
9
3565
蝶恋这首曲子来自著名RPG游戏仙剑奇侠传,很凄美的故事,每次女主角都会死
上一首曲子,我设计了乐曲的简单编码,一劳永逸,这首曲子,仅仅是换个数组的数据而已
相同的部分就不贴了,乐谱如下:
music = [\
23,0.5, 23,0.5, 23,0.5, 22,0.5, 23,2,\
22,0.5, 23,0.5, 22,0.5, 22,0.5, 16,1, 16,0.5, 17,0.5,\
21,1, 22,0.5, 21,0.5, 17,1, 16,0.5, 15,0.5,\
16,4,\
23,0.5, 23,0.5, 23,0.5, 22,0.5, 23,1.5, 26,0.5,\
25,0.5, 26,0.5, 25,0.5, 25,0.5, 22,1, 22,0.5, 23,0.5,\
24,1, 25,0.5, 24,0.5, 23,1, 22,0.5, 21,0.5,\
23,3.5, 23,0.5,\
26,1, 27,0.5, 26,0.5, 25,1.5, 23,0.5,\
25,3, 23,0.5, 25,0.5,\
22,1, 26,0.5, 25,0.5, 23,1, 22,0.5, 23,0.5,\
23,4,\
22,1, 26,0.5, 26,2.5,\
21,1, 26,0.5, 26,1.5, 26,0.5, 27,0.5,\
31,1, 27,0.5, 26,0.5, 27,1, 26,0.5, 27,0.5,\
23,3.5, 23,0.5,\
26,1, 27,0.5, 26,0.5, 25,1.5, 23,0.5,\
25,3, 23,0.5, 25,0.5,\
26,1, 27,0.5, 26,0.5, 27,1, 26,0.5, 27,0.5,\
23,4,\
22,1, 26,0.5, 26,2.5,\
21,1, 26,0.5, 26,1.5, 26,0.5, 27,0.5,\
31,1, 27,0.5, 26,0.5, 27,1, 25,1,\
26,4]
比较长,干脆一个小节一行,要是输错了,找起来方便
接下来是动作,就让S1来个心形曲线走位吧,查了一下,心形曲线是这样的
极坐标方程为ρ=a(1+cosθ)
化成参数方程:x=a(1+cosθ)cosθ, y=a(1+cosθ)sinθ
求导得到速度向量,有些生疏了,偷个懒,用神器 https://www.wolframalpha.com
x=-asinθ(2cosθ+1), y=a(cosθ + cos^2(θ) - sin^2(θ))
θ是时间的函数,我写了一个Move函数,计算速度向量,然后用麦轮的全向移动
变量t1是调出来的,可以改变初始位置和角速度
def Move(t,v):
t1 = t*0.48+math.pi
cost=math.cos(t1)
sint=math.sin(t1)
x=v*(-sint*(2*cost+1))
y=v*(-sint*sint+cost*cost+cost)
chassis_ctrl.move_with_speed(x,y,0)
然后是灯效,准备做一个绚丽的,以前玩Ardunio的时候看过一个例子,一圈LED变色旋转,我找到了那个程序,修改了一下,写成一个函数。原理我也没整明白,就是依葫芦画瓢翻译过来而已
def LED(t):
t2 = int(t * 20)
i = t2 % 8 + 1
j = t2 // 8 % 256
w = 255 - ( i * 32 + j ) % 256
if w < 85:
[r,g,b] = [255 - w * 3,0,w * 3]
else:
if w < 170:
w -= 85
[r,g,b] = [0,w * 3,255 - w * 3]
else:
w -= 170
[r,g,b] = [w * 3,255 - w * 3,0]
led_ctrl.set_top_led(rm_define.armor_top_all, r, g, b, rm_define.effect_always_off)
k=(i+3)%8+1
led_ctrl.set_single_led(rm_define.armor_top_all, [i,k], rm_define.effect_always_on)
最后,在主程序中调用这两个函数,over
def start():
global note
global music
v = 0.32 #指定线速度,越大越快,画的心形也越大
led_ctrl.set_bottom_led(rm_define.armor_bottom_all, 224, 0, 255, rm_define.effect_always_on)
tools.timer_ctrl(rm_define.timer_start)
for i in range(0,len(music)//2):
play_note(music[2*i])
t0 = tools.timer_current()
t = t0
while t < t0 + music[2*i+1]*0.8:
t = tools.timer_current()
Move(t,v)
LED(t)
chassis_ctrl.stop()
基本上写成模板了,音乐可以批量生产了:D
有两个地方希望官方改进(算报告bug吗?:D)
1. 播放音符的时间是固定的,大约0.2秒,要播更长的音,比如0.5秒,用等待的话,效果就是前0.2秒有声音,后0.3秒没声音。旋律是出来了,但是效果大打折扣。对蝶恋这种比较凄美的曲子来说更明显
2. 云台的8个灯,可以指定亮哪几个,但是都必须亮一样的颜色,效果大打折扣。希望函数led_ctrl.set_single_led()能够指定颜色
视频:
完整代码:
global note
note = [\
rm_define.media_sound_solmization_1C,rm_define.media_sound_solmization_1CSharp,\
rm_define.media_sound_solmization_1D,rm_define.media_sound_solmization_1DSharp,\
rm_define.media_sound_solmization_1E,\
rm_define.media_sound_solmization_1F,rm_define.media_sound_solmization_1FSharp,\
rm_define.media_sound_solmization_1G,rm_define.media_sound_solmization_1GSharp,\
rm_define.media_sound_solmization_1A,rm_define.media_sound_solmization_1ASharp,\
rm_define.media_sound_solmization_1B,\
rm_define.media_sound_solmization_2C,rm_define.media_sound_solmization_2CSharp,\
rm_define.media_sound_solmization_2D,rm_define.media_sound_solmization_2DSharp,\
rm_define.media_sound_solmization_2E,\
rm_define.media_sound_solmization_2F,rm_define.media_sound_solmization_2FSharp,\
rm_define.media_sound_solmization_2G,rm_define.media_sound_solmization_2GSharp,\
rm_define.media_sound_solmization_2A,rm_define.media_sound_solmization_2ASharp,\
rm_define.media_sound_solmization_2B,\
rm_define.media_sound_solmization_3C,rm_define.media_sound_solmization_3CSharp,\
rm_define.media_sound_solmization_3D,rm_define.media_sound_solmization_3DSharp,\
rm_define.media_sound_solmization_3E,\
rm_define.media_sound_solmization_3F,rm_define.media_sound_solmization_3FSharp,\
rm_define.media_sound_solmization_3G,rm_define.media_sound_solmization_3GSharp,\
rm_define.media_sound_solmization_3A,rm_define.media_sound_solmization_3ASharp,\
rm_define.media_sound_solmization_3B]
music = [\
23,0.5, 23,0.5, 23,0.5, 22,0.5, 23,2,\
22,0.5, 23,0.5, 22,0.5, 22,0.5, 16,1, 16,0.5, 17,0.5,\
21,1, 22,0.5, 21,0.5, 17,1, 16,0.5, 15,0.5,\
16,4,\
23,0.5, 23,0.5, 23,0.5, 22,0.5, 23,1.5, 26,0.5,\
25,0.5, 26,0.5, 25,0.5, 25,0.5, 22,1, 22,0.5, 23,0.5,\
24,1, 25,0.5, 24,0.5, 23,1, 22,0.5, 21,0.5,\
23,3.5, 23,0.5,\
26,1, 27,0.5, 26,0.5, 25,1.5, 23,0.5,\
25,3, 23,0.5, 25,0.5,\
22,1, 26,0.5, 25,0.5, 23,1, 22,0.5, 23,0.5,\
23,4,\
22,1, 26,0.5, 26,2.5,\
21,1, 26,0.5, 26,1.5, 26,0.5, 27,0.5,\
31,1, 27,0.5, 26,0.5, 27,1, 26,0.5, 27,0.5,\
23,3.5, 23,0.5,\
26,1, 27,0.5, 26,0.5, 25,1.5, 23,0.5,\
25,3, 23,0.5, 25,0.5,\
26,1, 27,0.5, 26,0.5, 27,1, 26,0.5, 27,0.5,\
23,4,\
22,1, 26,0.5, 26,2.5,\
21,1, 26,0.5, 26,1.5, 26,0.5, 27,0.5,\
31,1, 27,0.5, 26,0.5, 27,1, 25,1,\
26,4]
def play_note(x):
a = x // 10 - 1
b = x * 2 % 20 - 2
if b > 5:
b -= 1
n = int(a * 12 + b)
if x != 0:
media_ctrl.play_sound(note[n])
def start():
global note
global music
v = 0.32
led_ctrl.set_bottom_led(rm_define.armor_bottom_all, 224, 0, 255, rm_define.effect_always_on)
tools.timer_ctrl(rm_define.timer_start)
for i in range(0,len(music)//2):
play_note(music[2*i])
t0 = tools.timer_current()
t = t0
while t < t0 + music[2*i+1]*0.8:
t = tools.timer_current()
Move(t,v)
LED(t)
chassis_ctrl.stop()
def Move(t,v):
t1 = t*0.48+math.pi
cost=math.cos(t1)
sint=math.sin(t1)
x=v*(-sint*(2*cost+1))
y=v*(-sint*sint+cost*cost+cost)
chassis_ctrl.move_with_speed(x,y,0)
def LED(t):
t2 = int(t * 20)
i = t2 % 8 + 1
j = t2 // 8 % 256
w = 255 - ( i * 32 + j ) % 256
if w < 85:
[r,g,b] = [255 - w * 3,0,w * 3]
else:
if w < 170:
w -= 85
[r,g,b] = [0,w * 3,255 - w * 3]
else:
w -= 170
[r,g,b] = [w * 3,255 - w * 3,0]
led_ctrl.set_top_led(rm_define.armor_top_all, r, g, b, rm_define.effect_always_off)
k=(i+3)%8+1
led_ctrl.set_single_led(rm_define.armor_top_all, [i,k], rm_define.effect_always_on)
|