Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@0:6d54a81f6e58, 2011-12-01 (annotated)
- Committer:
- PandaHouse
- Date:
- Thu Dec 01 08:38:18 2011 +0000
- Revision:
- 0:6d54a81f6e58
- Child:
- 1:b9dfa2be76db
Futaba RS30X RS485 sample
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| PandaHouse | 0:6d54a81f6e58 | 1 | #include "mbed.h" |
| PandaHouse | 0:6d54a81f6e58 | 2 | Serial device(p9, p10); // tx = P9, rx = P10 |
| PandaHouse | 0:6d54a81f6e58 | 3 | DigitalOut REDE(p11); // RS485 Transmit Enable |
| PandaHouse | 0:6d54a81f6e58 | 4 | /*-------------------------------------------------*/ |
| PandaHouse | 0:6d54a81f6e58 | 5 | /* Funcyion : mbed initialize */ |
| PandaHouse | 0:6d54a81f6e58 | 6 | /* NAME : init */ |
| PandaHouse | 0:6d54a81f6e58 | 7 | /* Argument : --- */ |
| PandaHouse | 0:6d54a81f6e58 | 8 | /* Return value : --- */ |
| PandaHouse | 0:6d54a81f6e58 | 9 | /*-------------------------------------------------*/ |
| PandaHouse | 0:6d54a81f6e58 | 10 | void init(void){ |
| PandaHouse | 0:6d54a81f6e58 | 11 | device.baud(115200); // baud Rate = 115.2kbps [Futaba default] |
| PandaHouse | 0:6d54a81f6e58 | 12 | REDE = 0; // RS485 Transmit disable |
| PandaHouse | 0:6d54a81f6e58 | 13 | } |
| PandaHouse | 0:6d54a81f6e58 | 14 | /*-------------------------------------------------*/ |
| PandaHouse | 0:6d54a81f6e58 | 15 | /* Funcyion : servo torque enable */ |
| PandaHouse | 0:6d54a81f6e58 | 16 | /* NAME : torque */ |
| PandaHouse | 0:6d54a81f6e58 | 17 | /* Argument : ID (Servo ID) */ |
| PandaHouse | 0:6d54a81f6e58 | 18 | /* : data (Torque enable) */ |
| PandaHouse | 0:6d54a81f6e58 | 19 | /* Return value : --- */ |
| PandaHouse | 0:6d54a81f6e58 | 20 | /*-------------------------------------------------*/ |
| PandaHouse | 0:6d54a81f6e58 | 21 | void torque (unsigned char ID, unsigned char data){ |
| PandaHouse | 0:6d54a81f6e58 | 22 | |
| PandaHouse | 0:6d54a81f6e58 | 23 | unsigned char TxData[9]; // TransmitByteData [9byte] |
| PandaHouse | 0:6d54a81f6e58 | 24 | unsigned char CheckSum = 0; // CheckSum calculation |
| PandaHouse | 0:6d54a81f6e58 | 25 | |
| PandaHouse | 0:6d54a81f6e58 | 26 | TxData[0] = 0xFA; // Header |
| PandaHouse | 0:6d54a81f6e58 | 27 | TxData[1] = 0xAF; // Header |
| PandaHouse | 0:6d54a81f6e58 | 28 | TxData[2] = ID; // ID |
| PandaHouse | 0:6d54a81f6e58 | 29 | TxData[3] = 0x00; // Flags |
| PandaHouse | 0:6d54a81f6e58 | 30 | TxData[4] = 0x24; // Address |
| PandaHouse | 0:6d54a81f6e58 | 31 | TxData[5] = 0x01; // Length |
| PandaHouse | 0:6d54a81f6e58 | 32 | TxData[6] = 0x01; // Count |
| PandaHouse | 0:6d54a81f6e58 | 33 | TxData[7] = data; // Data |
| PandaHouse | 0:6d54a81f6e58 | 34 | |
| PandaHouse | 0:6d54a81f6e58 | 35 | // CheckSum calculation |
| PandaHouse | 0:6d54a81f6e58 | 36 | for(int i=2; i<=7; i++){ |
| PandaHouse | 0:6d54a81f6e58 | 37 | CheckSum = CheckSum ^ TxData[i]; // XOR from ID to Data |
| PandaHouse | 0:6d54a81f6e58 | 38 | } |
| PandaHouse | 0:6d54a81f6e58 | 39 | |
| PandaHouse | 0:6d54a81f6e58 | 40 | TxData[8] = CheckSum; // Sum |
| PandaHouse | 0:6d54a81f6e58 | 41 | |
| PandaHouse | 0:6d54a81f6e58 | 42 | // Send Packet |
| PandaHouse | 0:6d54a81f6e58 | 43 | REDE = 1; // RS485 Transmit Enable |
| PandaHouse | 0:6d54a81f6e58 | 44 | for(int i=0; i<=8; i++){ |
| PandaHouse | 0:6d54a81f6e58 | 45 | device.putc(TxData[i]); |
| PandaHouse | 0:6d54a81f6e58 | 46 | } |
| PandaHouse | 0:6d54a81f6e58 | 47 | wait_us(250); // Wait for transmission |
| PandaHouse | 0:6d54a81f6e58 | 48 | REDE = 0; // RS485 Transmitt disable |
| PandaHouse | 0:6d54a81f6e58 | 49 | } |
| PandaHouse | 0:6d54a81f6e58 | 50 | /*-------------------------------------------------*/ |
| PandaHouse | 0:6d54a81f6e58 | 51 | /* Funcyion : servo goal position instruction */ |
| PandaHouse | 0:6d54a81f6e58 | 52 | /* NAME : GoalPosition */ |
| PandaHouse | 0:6d54a81f6e58 | 53 | /* Argument : ID (Servo ID) */ |
| PandaHouse | 0:6d54a81f6e58 | 54 | /* : data (Goal Position) */ |
| PandaHouse | 0:6d54a81f6e58 | 55 | /* Return value : --- */ |
| PandaHouse | 0:6d54a81f6e58 | 56 | /*-------------------------------------------------*/ |
| PandaHouse | 0:6d54a81f6e58 | 57 | void GoalPosition (unsigned char ID, int data){ |
| PandaHouse | 0:6d54a81f6e58 | 58 | |
| PandaHouse | 0:6d54a81f6e58 | 59 | unsigned char TxData[10]; // TransmitByteData [10byte] |
| PandaHouse | 0:6d54a81f6e58 | 60 | unsigned char CheckSum = 0; // CheckSum calculation |
| PandaHouse | 0:6d54a81f6e58 | 61 | |
| PandaHouse | 0:6d54a81f6e58 | 62 | TxData[0] = 0xFA; // Header |
| PandaHouse | 0:6d54a81f6e58 | 63 | TxData[1] = 0xAF; // Header |
| PandaHouse | 0:6d54a81f6e58 | 64 | TxData[2] = ID; // ID |
| PandaHouse | 0:6d54a81f6e58 | 65 | TxData[3] = 0x00; // Flags |
| PandaHouse | 0:6d54a81f6e58 | 66 | TxData[4] = 0x1E; // Address |
| PandaHouse | 0:6d54a81f6e58 | 67 | TxData[5] = 0x02; // Length |
| PandaHouse | 0:6d54a81f6e58 | 68 | TxData[6] = 0x01; // Count |
| PandaHouse | 0:6d54a81f6e58 | 69 | // Data |
| PandaHouse | 0:6d54a81f6e58 | 70 | TxData[7] = (unsigned char)0x00FF & data; // Low byte |
| PandaHouse | 0:6d54a81f6e58 | 71 | TxData[8] = (unsigned char)0x00FF & (data >> 8); // Hi byte |
| PandaHouse | 0:6d54a81f6e58 | 72 | |
| PandaHouse | 0:6d54a81f6e58 | 73 | // CheckSum calculation |
| PandaHouse | 0:6d54a81f6e58 | 74 | for(int i=2; i<=8; i++){ |
| PandaHouse | 0:6d54a81f6e58 | 75 | CheckSum = CheckSum ^ TxData[i]; // XOR from ID to Data |
| PandaHouse | 0:6d54a81f6e58 | 76 | } |
| PandaHouse | 0:6d54a81f6e58 | 77 | TxData[9] = CheckSum; // Sum |
| PandaHouse | 0:6d54a81f6e58 | 78 | // Send Packet |
| PandaHouse | 0:6d54a81f6e58 | 79 | REDE = 1; // RS485 Transmitt Enable |
| PandaHouse | 0:6d54a81f6e58 | 80 | for(int i=0; i<=9; i++){ |
| PandaHouse | 0:6d54a81f6e58 | 81 | device.putc(TxData[i]); |
| PandaHouse | 0:6d54a81f6e58 | 82 | } |
| PandaHouse | 0:6d54a81f6e58 | 83 | wait_us(250); // Wait for transmission |
| PandaHouse | 0:6d54a81f6e58 | 84 | REDE = 0; // RS485 Transmit disable |
| PandaHouse | 0:6d54a81f6e58 | 85 | } |
| PandaHouse | 0:6d54a81f6e58 | 86 | /*-------------------------------------------------*/ |
| PandaHouse | 0:6d54a81f6e58 | 87 | /* Funcyion : main program */ |
| PandaHouse | 0:6d54a81f6e58 | 88 | /* NAME : main */ |
| PandaHouse | 0:6d54a81f6e58 | 89 | /* Argument : --- */ |
| PandaHouse | 0:6d54a81f6e58 | 90 | /* Return value : --- */ |
| PandaHouse | 0:6d54a81f6e58 | 91 | /*-------------------------------------------------*/ |
| PandaHouse | 0:6d54a81f6e58 | 92 | int main() { |
| PandaHouse | 0:6d54a81f6e58 | 93 | init(); // initialize |
| PandaHouse | 0:6d54a81f6e58 | 94 | torque(0x01, 0x01); // ID = 1(0x01) , torque = OFF (0x00) |
| PandaHouse | 0:6d54a81f6e58 | 95 | // torque = OFF(0x00), ON(0x01), BRAKE(0x02) |
| PandaHouse | 0:6d54a81f6e58 | 96 | wait(1); // wait (1sec) |
| PandaHouse | 0:6d54a81f6e58 | 97 | while(1){ |
| PandaHouse | 0:6d54a81f6e58 | 98 | GoalPosition(0x01,300); // ID = 1(0x01) , GoalPosition = 30.0deg(300) |
| PandaHouse | 0:6d54a81f6e58 | 99 | wait(1); // wait (1sec) |
| PandaHouse | 0:6d54a81f6e58 | 100 | GoalPosition(0x01,-300);// ID = 1(0x01) , GoalPosition = -30.0deg(-300) |
| PandaHouse | 0:6d54a81f6e58 | 101 | wait(1); // wait (1sec) |
| PandaHouse | 0:6d54a81f6e58 | 102 | } |
| PandaHouse | 0:6d54a81f6e58 | 103 | } |