網誌分類:Arduino |
圖片
網誌日期:2009-08-30 16:00



|
const int COMMAND_LENGTH = 3; const byte MASK_BITS[] = {B11111111, B11111111, B10000000}; // mask control the active bits // the commands const byte STOP[] = {B00000000, B11110100, B00000000}; // 00000000111101000 const byte TURBO[] = {B10110000, B01010100, B00000000}; // 10110000010101000 const byte FORWARD[] = {B10110000, B01010100, B00000000}; // 10110000010101000 const byte BACKWARD[] = {B10110000, B01010100, B00000000}; // 10110000010101000 const byte LEFT[] = {B00010000, B01100100, B00000000}; // 00010000011001000 const byte RIGHT[] = {B00010000, B10010100, B00000000}; // 00010000100101000 const byte RIGHT_FORWARD[] = {B00010000, B00010100, B00000000}; // 00010000000101000 const byte RIGHT_BACKWARD[] = {B00010000, B00100100, B00000000}; // 00010000001001000 const byte LEFT_FORWARD[] = {B00010000, B01000100, B00000000}; // 00010000010001000 const byte LEFT_BACKWARD[] = {B00010000, B10000100, B00000000}; // 00010000100001000 const byte CANNONRIGHT[] = {B00011000, B00000100, B00000000}; // 00011000000001000 const byte CANNONLEFT[] = {B00010100, B00000100, B00000000}; // 00010100000001000 const byte CANNONUP[] = {B00010001, B00000100, B00000000}; // 00010001000001000 const byte CANNONFIRE[] = {B00010010, B00000100, B00000000}; // 00010010000001000 |

The simple protocols sent from PyS60
|
msg = bytecmd(65) # "A" the sync byte |
// decode the protocols in the Arduino
loop until the byte matches the sync byte
|
int
findPacketHead() { |
if the value is grater than 127, it means it is a negative number
|
cmd[0] = receiveByte(); // mode cmd[1] = receiveByte(); // x-axis if (cmd[1] > 127) cmd[1] = cmd[1] -256; // if is it larger than 127, it is negative number cmd[2] = receiveByte(); // z-axis if (cmd[2] > 127) cmd[2] = cmd[2] -256; |
According to the following table, the select_command()
will select the command. ? means don't care the number.
|
Mode |
Z-axis |
X-axis |
Command |
|
0 |
? |
>30 |
Backward |
|
0 |
? |
<-30 |
Forward |
|
0 |
30 |
? |
Right |
|
0 |
<-30 |
? |
Left |
|
0 |
>30 |
>30 |
Forward_right |
|
0 |
<-30 |
>30 |
Forward_left |
|
0 |
<-30 |
<-30 |
Backward_right |
|
0 |
>30 |
<-30 |
Backward_left |
|
1 |
>30 |
? |
Turret_right |
|
1 |
<-30 |
? |
Turret_left |
|
1 |
? |
>30 |
Cannon_up |
|
1 |
? |
<-30 |
Fire |
|
? |
<30 |
<-30 |
Stop |
|
int select_command(){ delay_time=25; if(cmd[0] == '8') { // key 8 send_command(FORWARD); }else if(cmd[0] == '2') { // key 2 send_command(BACKWARD); }else if(cmd[0] == '4') { // key 4 send_command(LEFT); }else if(cmd[0] == '6') { // key 6 send_command(RIGHT); }else if(cmd[0] == '7') { // key 7 send_command(LEFT_FORWARD); }else if(cmd[0] == '9') { // key 9 send_command(RIGHT_FORWARD); }else if(cmd[0] == '1') { // key 1 send_command(LEFT_BACKWARD); }else if(cmd[0] == '3') { // key 3 send_command(RIGHT_BACKWARD); }else if(cmd[0] == '5') { // key 5 send_command(STOP); }else if(cmd[0] == 'q') { // key q send_command(CANNONLEFT); }else if(cmd[0] == 'w') { // key w send_command(CANNONRIGHT); }else if(cmd[0] == 'a') { // key a send_command(CANNONUP); }else if(cmd[0] == 's') { // key s send_command(CANNONFIRE); } } |
M-Blocks 2009-09-17 12:09
Blood Keith 分身 2009-09-16 23:07
http://bloodavkeith.spaces.live.com
Blood Keith 分身 2009-09-16 09:19
M-Blocks2009-09-16 20:52