Code to set up an AX-12 or MX-28 (and to scan for unidentified servos) ready for use in the robot arm.

Dependencies:   mbed

Committer:
jah128
Date:
Thu Feb 16 20:45:33 2017 +0000
Revision:
0:03f84c95f73b
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jah128 0:03f84c95f73b 1 /* NB This is part of an older MBED library, included so the older code runs on the
jah128 0:03f84c95f73b 2 * newer MBED library. There is probably a better implementation.... - jah */
jah128 0:03f84c95f73b 3
jah128 0:03f84c95f73b 4 /* mbed Microcontroller Library
jah128 0:03f84c95f73b 5 * Copyright (c) 2006-2012 ARM Limited
jah128 0:03f84c95f73b 6 *
jah128 0:03f84c95f73b 7 * Permission is hereby granted, free of charge, to any person obtaining a copy
jah128 0:03f84c95f73b 8 * of this software and associated documentation files (the "Software"), to deal
jah128 0:03f84c95f73b 9 * in the Software without restriction, including without limitation the rights
jah128 0:03f84c95f73b 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
jah128 0:03f84c95f73b 11 * copies of the Software, and to permit persons to whom the Software is
jah128 0:03f84c95f73b 12 * furnished to do so, subject to the following conditions:
jah128 0:03f84c95f73b 13 *
jah128 0:03f84c95f73b 14 * The above copyright notice and this permission notice shall be included in
jah128 0:03f84c95f73b 15 * all copies or substantial portions of the Software.
jah128 0:03f84c95f73b 16 *
jah128 0:03f84c95f73b 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jah128 0:03f84c95f73b 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jah128 0:03f84c95f73b 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
jah128 0:03f84c95f73b 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jah128 0:03f84c95f73b 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jah128 0:03f84c95f73b 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
jah128 0:03f84c95f73b 23 * SOFTWARE.
jah128 0:03f84c95f73b 24 *
jah128 0:03f84c95f73b 25 * NOTE: This is an unsupported legacy untested library.
jah128 0:03f84c95f73b 26 */
jah128 0:03f84c95f73b 27 #ifndef MBED_SERIALHALFDUPLEX_H
jah128 0:03f84c95f73b 28 #define MBED_SERIALHALFDUPLEX_H
jah128 0:03f84c95f73b 29
jah128 0:03f84c95f73b 30 #include "device.h"
jah128 0:03f84c95f73b 31
jah128 0:03f84c95f73b 32 #if DEVICE_SERIAL
jah128 0:03f84c95f73b 33
jah128 0:03f84c95f73b 34 #include "Serial.h"
jah128 0:03f84c95f73b 35 #include "PinNames.h"
jah128 0:03f84c95f73b 36 #include "PeripheralNames.h"
jah128 0:03f84c95f73b 37
jah128 0:03f84c95f73b 38 namespace mbed {
jah128 0:03f84c95f73b 39
jah128 0:03f84c95f73b 40 /* Class: SerialHalfDuplex
jah128 0:03f84c95f73b 41 * A serial port (UART) for communication with other devices using
jah128 0:03f84c95f73b 42 * Half-Duplex, allowing transmit and receive on a single
jah128 0:03f84c95f73b 43 * shared transmit and receive line. Only one end should be transmitting
jah128 0:03f84c95f73b 44 * at a time.
jah128 0:03f84c95f73b 45 *
jah128 0:03f84c95f73b 46 * Both the tx and rx pin should be defined, and wired together.
jah128 0:03f84c95f73b 47 * This is in addition to them being wired to the other serial
jah128 0:03f84c95f73b 48 * device to allow both read and write functions to operate.
jah128 0:03f84c95f73b 49 *
jah128 0:03f84c95f73b 50 * Example:
jah128 0:03f84c95f73b 51 * > // Send a byte to a second HalfDuplex device, and read the response
jah128 0:03f84c95f73b 52 * >
jah128 0:03f84c95f73b 53 * > #include "mbed.h"
jah128 0:03f84c95f73b 54 * >
jah128 0:03f84c95f73b 55 * > // p9 and p10 should be wired together to form "a"
jah128 0:03f84c95f73b 56 * > // p28 and p27 should be wired together to form "b"
jah128 0:03f84c95f73b 57 * > // p9/p10 should be wired to p28/p27 as the Half Duplex connection
jah128 0:03f84c95f73b 58 * >
jah128 0:03f84c95f73b 59 * > SerialHalfDuplex a(p9, p10);
jah128 0:03f84c95f73b 60 * > SerialHalfDuplex b(p28, p27);
jah128 0:03f84c95f73b 61 * >
jah128 0:03f84c95f73b 62 * > void b_rx() { // second device response
jah128 0:03f84c95f73b 63 * > b.putc(b.getc() + 4);
jah128 0:03f84c95f73b 64 * > }
jah128 0:03f84c95f73b 65 * >
jah128 0:03f84c95f73b 66 * > int main() {
jah128 0:03f84c95f73b 67 * > b.attach(&b_rx);
jah128 0:03f84c95f73b 68 * > for(int c = 'A'; c < 'Z'; c++) {
jah128 0:03f84c95f73b 69 * > a.putc(c);
jah128 0:03f84c95f73b 70 * > printf("sent [%c]\n", c);
jah128 0:03f84c95f73b 71 * > wait(0.5); // b should respond
jah128 0:03f84c95f73b 72 * > if(a.readable()) {
jah128 0:03f84c95f73b 73 * > printf("received [%c]\n", a.getc());
jah128 0:03f84c95f73b 74 * > }
jah128 0:03f84c95f73b 75 * > }
jah128 0:03f84c95f73b 76 * > }
jah128 0:03f84c95f73b 77 *
jah128 0:03f84c95f73b 78 * For Simplex and Full-Duplex Serial communication, see <Serial>
jah128 0:03f84c95f73b 79 */
jah128 0:03f84c95f73b 80 class SerialHalfDuplex : public Serial {
jah128 0:03f84c95f73b 81
jah128 0:03f84c95f73b 82 public:
jah128 0:03f84c95f73b 83 /* Constructor: SerialHalfDuplex
jah128 0:03f84c95f73b 84 * Create a half-duplex serial port, connected to the specified transmit
jah128 0:03f84c95f73b 85 * and receive pins.
jah128 0:03f84c95f73b 86 *
jah128 0:03f84c95f73b 87 * These pins should be wired together, as well as to the target device
jah128 0:03f84c95f73b 88 *
jah128 0:03f84c95f73b 89 * Variables:
jah128 0:03f84c95f73b 90 * tx - Transmit pin
jah128 0:03f84c95f73b 91 * rx - Receive pin
jah128 0:03f84c95f73b 92 */
jah128 0:03f84c95f73b 93 SerialHalfDuplex(PinName tx, PinName rx, const char *name = NULL);
jah128 0:03f84c95f73b 94
jah128 0:03f84c95f73b 95 #if 0 // Inherited from Serial class, for documentation
jah128 0:03f84c95f73b 96 /* Function: baud
jah128 0:03f84c95f73b 97 * Set the baud rate of the serial port
jah128 0:03f84c95f73b 98 *
jah128 0:03f84c95f73b 99 * Variables:
jah128 0:03f84c95f73b 100 * baudrate - The baudrate of the serial port (default = 9600).
jah128 0:03f84c95f73b 101 */
jah128 0:03f84c95f73b 102 void baud(int baudrate);
jah128 0:03f84c95f73b 103
jah128 0:03f84c95f73b 104 enum Parity {
jah128 0:03f84c95f73b 105 None = 0
jah128 0:03f84c95f73b 106 , Odd
jah128 0:03f84c95f73b 107 , Even
jah128 0:03f84c95f73b 108 , Forced1
jah128 0:03f84c95f73b 109 , Forced0
jah128 0:03f84c95f73b 110 };
jah128 0:03f84c95f73b 111
jah128 0:03f84c95f73b 112 /* Function: format
jah128 0:03f84c95f73b 113 * Set the transmission format used by the Serial port
jah128 0:03f84c95f73b 114 *
jah128 0:03f84c95f73b 115 * Variables:
jah128 0:03f84c95f73b 116 * bits - The number of bits in a word (5-8; default = 8)
jah128 0:03f84c95f73b 117 * parity - The parity used (Serial::None, Serial::Odd,
jah128 0:03f84c95f73b 118 Serial::Even, Serial::Forced1, Serial::Forced0; default = Serial::None)
jah128 0:03f84c95f73b 119 * stop - The number of stop bits (1 or 2; default = 1)
jah128 0:03f84c95f73b 120 */
jah128 0:03f84c95f73b 121 void format(int bits = 8, Parity parity = Serial::None, int stop_bits
jah128 0:03f84c95f73b 122 = 1);
jah128 0:03f84c95f73b 123
jah128 0:03f84c95f73b 124 /* Function: putc
jah128 0:03f84c95f73b 125 * Write a character
jah128 0:03f84c95f73b 126 *
jah128 0:03f84c95f73b 127 * Variables:
jah128 0:03f84c95f73b 128 * c - The character to write to the serial port
jah128 0:03f84c95f73b 129 */
jah128 0:03f84c95f73b 130 int putc(int c);
jah128 0:03f84c95f73b 131
jah128 0:03f84c95f73b 132 /* Function: getc
jah128 0:03f84c95f73b 133 * Read a character
jah128 0:03f84c95f73b 134 *
jah128 0:03f84c95f73b 135 * Read a character from the serial port. This call will block
jah128 0:03f84c95f73b 136 * until a character is available. For testing if a character is
jah128 0:03f84c95f73b 137 * available for reading, see <readable>.
jah128 0:03f84c95f73b 138 *
jah128 0:03f84c95f73b 139 * Variables:
jah128 0:03f84c95f73b 140 * returns - The character read from the serial port
jah128 0:03f84c95f73b 141 */
jah128 0:03f84c95f73b 142 int getc();
jah128 0:03f84c95f73b 143
jah128 0:03f84c95f73b 144 /* Function: printf
jah128 0:03f84c95f73b 145 * Write a formated string
jah128 0:03f84c95f73b 146 *
jah128 0:03f84c95f73b 147 * Variables:
jah128 0:03f84c95f73b 148 * format - A printf-style format string, followed by the
jah128 0:03f84c95f73b 149 * variables to use in formating the string.
jah128 0:03f84c95f73b 150 */
jah128 0:03f84c95f73b 151 int printf(const char* format, ...);
jah128 0:03f84c95f73b 152
jah128 0:03f84c95f73b 153 /* Function: scanf
jah128 0:03f84c95f73b 154 * Read a formated string
jah128 0:03f84c95f73b 155 *
jah128 0:03f84c95f73b 156 * Variables:
jah128 0:03f84c95f73b 157 * format - A scanf-style format string,
jah128 0:03f84c95f73b 158 * followed by the pointers to variables to store the results.
jah128 0:03f84c95f73b 159 */
jah128 0:03f84c95f73b 160 int scanf(const char* format, ...);
jah128 0:03f84c95f73b 161
jah128 0:03f84c95f73b 162 /* Function: readable
jah128 0:03f84c95f73b 163 * Determine if there is a character available to read
jah128 0:03f84c95f73b 164 *
jah128 0:03f84c95f73b 165 * Variables:
jah128 0:03f84c95f73b 166 * returns - 1 if there is a character available to read, else 0
jah128 0:03f84c95f73b 167 */
jah128 0:03f84c95f73b 168 int readable();
jah128 0:03f84c95f73b 169
jah128 0:03f84c95f73b 170 /* Function: writeable
jah128 0:03f84c95f73b 171 * Determine if there is space available to write a character
jah128 0:03f84c95f73b 172 *
jah128 0:03f84c95f73b 173 * Variables:
jah128 0:03f84c95f73b 174 * returns - 1 if there is space to write a character, else 0
jah128 0:03f84c95f73b 175 */
jah128 0:03f84c95f73b 176 int writeable();
jah128 0:03f84c95f73b 177
jah128 0:03f84c95f73b 178 /* Function: attach
jah128 0:03f84c95f73b 179 * Attach a function to call whenever a serial interrupt is generated
jah128 0:03f84c95f73b 180 *
jah128 0:03f84c95f73b 181 * Variables:
jah128 0:03f84c95f73b 182 * fptr - A pointer to a void function, or 0 to set as none
jah128 0:03f84c95f73b 183 */
jah128 0:03f84c95f73b 184 void attach(void (*fptr)(void));
jah128 0:03f84c95f73b 185
jah128 0:03f84c95f73b 186 /* Function: attach
jah128 0:03f84c95f73b 187 * Attach a member function to call whenever a serial interrupt is generated
jah128 0:03f84c95f73b 188 *
jah128 0:03f84c95f73b 189 * Variables:
jah128 0:03f84c95f73b 190 * tptr - pointer to the object to call the member function on
jah128 0:03f84c95f73b 191 * mptr - pointer to the member function to be called
jah128 0:03f84c95f73b 192 */
jah128 0:03f84c95f73b 193 template<typename T>
jah128 0:03f84c95f73b 194 void attach(T* tptr, void (T::*mptr)(void));
jah128 0:03f84c95f73b 195
jah128 0:03f84c95f73b 196 #endif
jah128 0:03f84c95f73b 197
jah128 0:03f84c95f73b 198 protected:
jah128 0:03f84c95f73b 199 PinName _txpin;
jah128 0:03f84c95f73b 200
jah128 0:03f84c95f73b 201 virtual int _putc(int c);
jah128 0:03f84c95f73b 202 virtual int _getc(void);
jah128 0:03f84c95f73b 203
jah128 0:03f84c95f73b 204 }; // End class SerialHalfDuplex
jah128 0:03f84c95f73b 205
jah128 0:03f84c95f73b 206 } // End namespace
jah128 0:03f84c95f73b 207
jah128 0:03f84c95f73b 208 #endif
jah128 0:03f84c95f73b 209
jah128 0:03f84c95f73b 210 #endif