setAlias (设定别名)
概述
这个方法可以向许多致动器发送许多不同的定时命令。
可以通过两种方式进行:
- Time-mixed:给每个致动器定时命令发送一个特定的时间
- Time-separate:首先发送所有时间的列表(例如: 10ms,20ms,30ms,40ms),然后发送所有对应这些时间的命令 (如果有4个时间,那么就要向每个致动器发送4个命令)。 这个方式效率更高。
方法
/** * Call this function to send timed-command list to an alias * An alias is used to update many actuator (as with set) at the same time, * just giving the name of the alias, and not the name of actuators. * * @param pCommands ALValue with all data * pCommands[0] Is the Alias name * pCommands[1] Is the kind of update : * "Merge" : Just add new timed command. * "ClearAll" : Delete all timed command before adding this one. * "ClearAfter" : Delete all command that are after the time of the 1st one on the list (the soonest) * "ClearBefore" : Delete all command that are before the time of the last one on the list (the oldest) * pCommands[2] Type of command : could be "time-mixed" or "time-separate" * * For pParams[3] there is 2 options : * if it's "time-mixed" : * pCommands[3][x] is a list of x actuator. Each x is an actuator member of the alias * pCommands[3][x][y] is a list of y timed-command for this actuator * pCommands[3][x][y][0] is the float command (offset and gain are then used) * pCommands[3][x][y][1] is the DCM Time for the command to be applied * pCommands[3][x][y][2] is the optional importance level. Minimum if not specified * * For pCommands[3] there is 2 options : * if it's "time-separate" : * pCommands[3] is the importance level of all these commands * * pCommands[4] is a list of T time. * pCommands[4][0] If the 1st time * pCommands[4][1] If the 2nd time * pCommands[4][2] ... * pCommands[4][T] T time * * pCommands[5][x] is a list of x command list. Each x is an actuator member of the alias * pCommands[5][x][0] 1st command for this actuator, and at the 1st time * pCommands[5][x][1] 2nd command for this actuator, and at the 2nd time * pCommands[5][x][2] ... * pCommands[5][x][T] T command * * */ void DCM::setAlias(ALValue& pCommands)
使用实例
使用“Time-mixed”
Python语言:
import naoqi from naoqi import ALProxy dcm = ALProxy("DCM","127.0.0.1",9559) dcm.createAlias([ "ChestLeds", [ "ChestBoard/Led/Red/Actuator/Value", "ChestBoard/Led/Green/Actuator/Value", "ChestBoard/Led/Blue/Actuator/Value" ] ]) dcm.setAlias([ "ChestLeds", "ClearAll", "time-mixed", [ [ [1.0, dcm.getTime(4000)], [0.0, dcm.getTime(6000)] ], [ [0.25, dcm.getTime(3000)] ], [ [0.125,dcm.getTime(2000)] ] ] ])
C++语言:
// Program running in a module. Do not forget the #include "dcmproxy.h" try { DCMProxy* dcm = new DCMProxy(pBroker); ALValue commandsAlias; ALValue commands; commandsAlias.arraySetSize(2); commandsAlias[0] = string("ChestLeds"); commandsAlias[1].arraySetSize(3); commandsAlias[1][0] = string("ChestBoard/Led/Red/Actuator/Value"); commandsAlias[1][1] = string("ChestBoard/Led/Green/Actuator/Value"); commandsAlias[1][2] = string("ChestBoard/Led/Blue/Actuator/Value"); dcm->createAlias(commandsAlias); commands.arraySetSize(4); commands[0] = string("ChestLeds"); commands[1] = string("ClearAll"); commands[2] = string("time-mixed"); commands[3].arraySetSize(3); //ChestBoard/Led/Red/Actuator/Value commands[3][0].arraySetSize(2); commands[3][0][0].arraySetSize(2); commands[3][0][0][0] = 1.0; commands[3][0][0][1] = dcm->getTime(4000); commands[3][0][1].arraySetSize(2); commands[3][0][1][0] = 0.0; commands[3][0][1][1] = dcm->getTime(6000); //ChestBoard/Led/Green/Actuator/Value commands[3][1].arraySetSize(1); commands[3][1][0].arraySetSize(2); commands[3][1][0][0] = 0.25; commands[3][1][0][1] = dcm->getTime(3000); //ChestBoard/Led/Blue/Actuator/Value commands[3][2].arraySetSize(1); commands[3][2][0].arraySetSize(2); commands[3][2][0][0] = 0.125; commands[3][2][0][1] = dcm->getTime(2000); dcm->setAlias(commands); } AL_CATCH_ERR(return false;);
向红色发光二极管发送2个命令(值1.0在4s;值0.0 在6s),向另外2个LED发送1个命令(绿色:值0.25在3s;蓝色: 值0.125在2s)。
使用“Time-separate”:
Python语言:
import naoqi from naoqi import ALProxy dcm = ALProxy("DCM","127.0.0.1",9559) dcm.createAlias([ "ChestLeds", [ "ChestBoard/Led/Red/Actuator/Value", "ChestBoard/Led/Green/Actuator/Value", "ChestBoard/Led/Blue/Actuator/Value" ] ]) t = dcm.getTime(0) dcm.setAlias([ "ChestLeds", "ClearAll", "time-separate", 0, [t,t+2000, t+3000, t+4000, t+5000,t+6000], [ [1.0,0.0,1.0,0.0,1.0,0.0], [1.0,0.5,1.0,0.25,0.125,0.0], [0.0625,0.125,0.25,0.50,0.75,1.0] ] ])
C++语言:
// Program running in a module. Do not forget the #include "dcmproxy.h" try { DCMProxy* dcm = new DCMProxy(pBroker); ALValue commandsAlias; ALValue commands; commandsAlias.arraySetSize(2); commandsAlias[0] = string("ChestLeds"); commandsAlias[1].arraySetSize(3); commandsAlias[1][0] = string("ChestBoard/Led/Red/Actuator/Value"); commandsAlias[1][1] = string("ChestBoard/Led/Green/Actuator/Value"); commandsAlias[1][2] = string("ChestBoard/Led/Blue/Actuator/Value"); dcm->createAlias(commandsAlias); commands.arraySetSize(6); commands[0] = string("ChestLeds"); commands[1] = string("ClearAll"); commands[2] = string("time-separate"); commands[3] = 0; commands[4].arraySetSize(6); commands[4][0] = dcm->getTime(10000); commands[4][1] = dcm->getTime(20000); commands[4][2] = dcm->getTime(30000); commands[4][3] = dcm->getTime(40000); commands[4][4] = dcm->getTime(50000); commands[4][5] = dcm->getTime(60000); commands[5].arraySetSize(3); // ChestBoard/Led/Red/Actuator/Value commands[5][0].arraySetSize(6); commands[5][0][0] = 1.0; commands[5][0][1] = 0.0; commands[5][0][2] = 1.0; commands[5][0][3] = 0.0; commands[5][0][4] = 1.0; commands[5][0][5] = 0.0; // ChestBoard/Led/Green/Actuator/Value commands[5][1].arraySetSize(6); commands[5][1][0] = 1.0; commands[5][1][1] = 0.5; commands[5][1][2] = 1.0; commands[5][1][3] = 0.25; commands[5][1][4] = 0.125; commands[5][1][5] = 0.0; // ChestBoard/Led/Blue/Actuator/Value commands[5][2].arraySetSize(6); commands[5][2][0] = 0.0625; commands[5][2][1] = 0.125; commands[5][2][2] = 0.25; commands[5][2][3] = 0.50; commands[5][2][4] = 0.75; commands[5][2][5] = 1.0; dcm->setAlias(commands); } AL_CATCH_ERR(return false;);
向每个发光二极管发送6个命令,分别在10s、20s、30s、40s、50s和60s。
对于红色,命令为:1,0,1,0,1,0。
对于绿色,命令为:1,0.5,1.0,0.25,0.125,0。
对于蓝色,命令为:0.0625,0.125,0.25,0.50,0.75, 1.0。
注释: |
这里的所有范例都使用“dcm.getTime()”来获得定时命令请求的所有时间。 这只是一个例子,还有其它更好的使用方法。 例如,您可以在启动时只完成一次“dcm.getTime()”,然后增加每毫秒整数值。 或者,如果机器人上有一个模块在运行时,您也可以使用CPU时钟。 |
---|
注释: |
可以使用C++语言向致动器值发送一个“NAN”浮点数(“Not A Number”)(仅限在同一过程)。 “NAN”会被忽略。如果一个别名里包含许多致动器,可以将NAN命令发送至这一别名里的某些致动器。这样一来, 这些关节就不会动。 |
---|
注释: |
为了在C++语言里提高速度,最好不要在每次请求时都创建 AlValue,而是应该保留它,在发送至相同别名时只改变其中的值。 这是因为创建和调整“ALValue”都是繁重的操作,而改变一个已分配的值则仅是一个简单的操作。 |
---|