mbed robotracer for education.
Robotracer (line follower robot ) using stepper motor.
/media/uploads/hayama/mbedrobotracer-manual-english.pdf
/media/uploads/hayama/mbedrobotracer-manual-japanese.pdf
/media/uploads/hayama/eagle-design-robotracer.zip
movie -> https://www.youtube.com/watch?v=INwun8gSds4
(for competition->) https://www.youtube.com/watch?v=l_gP2pUt4w0
mbed/CAN.h@1:200aad416161, 2013-10-02 (annotated)
- Committer:
- hayama
- Date:
- Wed Oct 02 01:09:55 2013 +0000
- Revision:
- 1:200aad416161
- Parent:
- 0:da22b0b4395a
mbed robotracer for education.
;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hayama | 0:da22b0b4395a | 1 | /* mbed Microcontroller Library |
hayama | 0:da22b0b4395a | 2 | * Copyright (c) 2006-2013 ARM Limited |
hayama | 0:da22b0b4395a | 3 | * |
hayama | 0:da22b0b4395a | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
hayama | 0:da22b0b4395a | 5 | * you may not use this file except in compliance with the License. |
hayama | 0:da22b0b4395a | 6 | * You may obtain a copy of the License at |
hayama | 0:da22b0b4395a | 7 | * |
hayama | 0:da22b0b4395a | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
hayama | 0:da22b0b4395a | 9 | * |
hayama | 0:da22b0b4395a | 10 | * Unless required by applicable law or agreed to in writing, software |
hayama | 0:da22b0b4395a | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
hayama | 0:da22b0b4395a | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
hayama | 0:da22b0b4395a | 13 | * See the License for the specific language governing permissions and |
hayama | 0:da22b0b4395a | 14 | * limitations under the License. |
hayama | 0:da22b0b4395a | 15 | */ |
hayama | 0:da22b0b4395a | 16 | #ifndef MBED_CAN_H |
hayama | 0:da22b0b4395a | 17 | #define MBED_CAN_H |
hayama | 0:da22b0b4395a | 18 | |
hayama | 0:da22b0b4395a | 19 | #include "platform.h" |
hayama | 0:da22b0b4395a | 20 | |
hayama | 0:da22b0b4395a | 21 | #if DEVICE_CAN |
hayama | 0:da22b0b4395a | 22 | |
hayama | 0:da22b0b4395a | 23 | #include "can_api.h" |
hayama | 0:da22b0b4395a | 24 | #include "can_helper.h" |
hayama | 0:da22b0b4395a | 25 | #include "FunctionPointer.h" |
hayama | 0:da22b0b4395a | 26 | |
hayama | 0:da22b0b4395a | 27 | namespace mbed { |
hayama | 0:da22b0b4395a | 28 | |
hayama | 0:da22b0b4395a | 29 | /** CANMessage class |
hayama | 0:da22b0b4395a | 30 | */ |
hayama | 0:da22b0b4395a | 31 | class CANMessage : public CAN_Message { |
hayama | 0:da22b0b4395a | 32 | |
hayama | 0:da22b0b4395a | 33 | public: |
hayama | 0:da22b0b4395a | 34 | /** Creates empty CAN message. |
hayama | 0:da22b0b4395a | 35 | */ |
hayama | 0:da22b0b4395a | 36 | CANMessage() { |
hayama | 0:da22b0b4395a | 37 | len = 8; |
hayama | 0:da22b0b4395a | 38 | type = CANData; |
hayama | 0:da22b0b4395a | 39 | format = CANStandard; |
hayama | 0:da22b0b4395a | 40 | id = 0; |
hayama | 0:da22b0b4395a | 41 | memset(data, 0, 8); |
hayama | 0:da22b0b4395a | 42 | } |
hayama | 0:da22b0b4395a | 43 | |
hayama | 0:da22b0b4395a | 44 | /** Creates CAN message with specific content. |
hayama | 0:da22b0b4395a | 45 | */ |
hayama | 0:da22b0b4395a | 46 | CANMessage(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) { |
hayama | 0:da22b0b4395a | 47 | len = _len & 0xF; |
hayama | 0:da22b0b4395a | 48 | type = _type; |
hayama | 0:da22b0b4395a | 49 | format = _format; |
hayama | 0:da22b0b4395a | 50 | id = _id; |
hayama | 0:da22b0b4395a | 51 | memcpy(data, _data, _len); |
hayama | 0:da22b0b4395a | 52 | } |
hayama | 0:da22b0b4395a | 53 | |
hayama | 0:da22b0b4395a | 54 | /** Creates CAN remote message. |
hayama | 0:da22b0b4395a | 55 | */ |
hayama | 0:da22b0b4395a | 56 | CANMessage(int _id, CANFormat _format = CANStandard) { |
hayama | 0:da22b0b4395a | 57 | len = 0; |
hayama | 0:da22b0b4395a | 58 | type = CANRemote; |
hayama | 0:da22b0b4395a | 59 | format = _format; |
hayama | 0:da22b0b4395a | 60 | id = _id; |
hayama | 0:da22b0b4395a | 61 | memset(data, 0, 8); |
hayama | 0:da22b0b4395a | 62 | } |
hayama | 0:da22b0b4395a | 63 | }; |
hayama | 0:da22b0b4395a | 64 | |
hayama | 0:da22b0b4395a | 65 | /** A can bus client, used for communicating with can devices |
hayama | 0:da22b0b4395a | 66 | */ |
hayama | 0:da22b0b4395a | 67 | class CAN { |
hayama | 0:da22b0b4395a | 68 | |
hayama | 0:da22b0b4395a | 69 | public: |
hayama | 0:da22b0b4395a | 70 | /** Creates an CAN interface connected to specific pins. |
hayama | 0:da22b0b4395a | 71 | * |
hayama | 0:da22b0b4395a | 72 | * @param rd read from transmitter |
hayama | 0:da22b0b4395a | 73 | * @param td transmit to transmitter |
hayama | 0:da22b0b4395a | 74 | * |
hayama | 0:da22b0b4395a | 75 | * Example: |
hayama | 0:da22b0b4395a | 76 | * @code |
hayama | 0:da22b0b4395a | 77 | * #include "mbed.h" |
hayama | 0:da22b0b4395a | 78 | * |
hayama | 0:da22b0b4395a | 79 | * Ticker ticker; |
hayama | 0:da22b0b4395a | 80 | * DigitalOut led1(LED1); |
hayama | 0:da22b0b4395a | 81 | * DigitalOut led2(LED2); |
hayama | 0:da22b0b4395a | 82 | * CAN can1(p9, p10); |
hayama | 0:da22b0b4395a | 83 | * CAN can2(p30, p29); |
hayama | 0:da22b0b4395a | 84 | * |
hayama | 0:da22b0b4395a | 85 | * char counter = 0; |
hayama | 0:da22b0b4395a | 86 | * |
hayama | 0:da22b0b4395a | 87 | * void send() { |
hayama | 0:da22b0b4395a | 88 | * if(can1.write(CANMessage(1337, &counter, 1))) { |
hayama | 0:da22b0b4395a | 89 | * printf("Message sent: %d\n", counter); |
hayama | 0:da22b0b4395a | 90 | * counter++; |
hayama | 0:da22b0b4395a | 91 | * } |
hayama | 0:da22b0b4395a | 92 | * led1 = !led1; |
hayama | 0:da22b0b4395a | 93 | * } |
hayama | 0:da22b0b4395a | 94 | * |
hayama | 0:da22b0b4395a | 95 | * int main() { |
hayama | 0:da22b0b4395a | 96 | * ticker.attach(&send, 1); |
hayama | 0:da22b0b4395a | 97 | * CANMessage msg; |
hayama | 0:da22b0b4395a | 98 | * while(1) { |
hayama | 0:da22b0b4395a | 99 | * if(can2.read(msg)) { |
hayama | 0:da22b0b4395a | 100 | * printf("Message received: %d\n\n", msg.data[0]); |
hayama | 0:da22b0b4395a | 101 | * led2 = !led2; |
hayama | 0:da22b0b4395a | 102 | * } |
hayama | 0:da22b0b4395a | 103 | * wait(0.2); |
hayama | 0:da22b0b4395a | 104 | * } |
hayama | 0:da22b0b4395a | 105 | * } |
hayama | 0:da22b0b4395a | 106 | * @endcode |
hayama | 0:da22b0b4395a | 107 | */ |
hayama | 0:da22b0b4395a | 108 | CAN(PinName rd, PinName td); |
hayama | 0:da22b0b4395a | 109 | virtual ~CAN(); |
hayama | 0:da22b0b4395a | 110 | |
hayama | 0:da22b0b4395a | 111 | /** Set the frequency of the CAN interface |
hayama | 0:da22b0b4395a | 112 | * |
hayama | 0:da22b0b4395a | 113 | * @param hz The bus frequency in hertz |
hayama | 0:da22b0b4395a | 114 | * |
hayama | 0:da22b0b4395a | 115 | * @returns |
hayama | 0:da22b0b4395a | 116 | * 1 if successful, |
hayama | 0:da22b0b4395a | 117 | * 0 otherwise |
hayama | 0:da22b0b4395a | 118 | */ |
hayama | 0:da22b0b4395a | 119 | int frequency(int hz); |
hayama | 0:da22b0b4395a | 120 | |
hayama | 0:da22b0b4395a | 121 | /** Write a CANMessage to the bus. |
hayama | 0:da22b0b4395a | 122 | * |
hayama | 0:da22b0b4395a | 123 | * @param msg The CANMessage to write. |
hayama | 0:da22b0b4395a | 124 | * |
hayama | 0:da22b0b4395a | 125 | * @returns |
hayama | 0:da22b0b4395a | 126 | * 0 if write failed, |
hayama | 0:da22b0b4395a | 127 | * 1 if write was successful |
hayama | 0:da22b0b4395a | 128 | */ |
hayama | 0:da22b0b4395a | 129 | int write(CANMessage msg); |
hayama | 0:da22b0b4395a | 130 | |
hayama | 0:da22b0b4395a | 131 | /** Read a CANMessage from the bus. |
hayama | 0:da22b0b4395a | 132 | * |
hayama | 0:da22b0b4395a | 133 | * @param msg A CANMessage to read to. |
hayama | 0:da22b0b4395a | 134 | * |
hayama | 0:da22b0b4395a | 135 | * @returns |
hayama | 0:da22b0b4395a | 136 | * 0 if no message arrived, |
hayama | 0:da22b0b4395a | 137 | * 1 if message arrived |
hayama | 0:da22b0b4395a | 138 | */ |
hayama | 0:da22b0b4395a | 139 | int read(CANMessage &msg); |
hayama | 0:da22b0b4395a | 140 | |
hayama | 0:da22b0b4395a | 141 | /** Reset CAN interface. |
hayama | 0:da22b0b4395a | 142 | * |
hayama | 0:da22b0b4395a | 143 | * To use after error overflow. |
hayama | 0:da22b0b4395a | 144 | */ |
hayama | 0:da22b0b4395a | 145 | void reset(); |
hayama | 0:da22b0b4395a | 146 | |
hayama | 0:da22b0b4395a | 147 | /** Puts or removes the CAN interface into silent monitoring mode |
hayama | 0:da22b0b4395a | 148 | * |
hayama | 0:da22b0b4395a | 149 | * @param silent boolean indicating whether to go into silent mode or not |
hayama | 0:da22b0b4395a | 150 | */ |
hayama | 0:da22b0b4395a | 151 | void monitor(bool silent); |
hayama | 0:da22b0b4395a | 152 | |
hayama | 0:da22b0b4395a | 153 | /** Returns number of read errors to detect read overflow errors. |
hayama | 0:da22b0b4395a | 154 | */ |
hayama | 0:da22b0b4395a | 155 | unsigned char rderror(); |
hayama | 0:da22b0b4395a | 156 | |
hayama | 0:da22b0b4395a | 157 | /** Returns number of write errors to detect write overflow errors. |
hayama | 0:da22b0b4395a | 158 | */ |
hayama | 0:da22b0b4395a | 159 | unsigned char tderror(); |
hayama | 0:da22b0b4395a | 160 | |
hayama | 0:da22b0b4395a | 161 | /** Attach a function to call whenever a CAN frame received interrupt is |
hayama | 0:da22b0b4395a | 162 | * generated. |
hayama | 0:da22b0b4395a | 163 | * |
hayama | 0:da22b0b4395a | 164 | * @param fptr A pointer to a void function, or 0 to set as none |
hayama | 0:da22b0b4395a | 165 | */ |
hayama | 0:da22b0b4395a | 166 | void attach(void (*fptr)(void)); |
hayama | 0:da22b0b4395a | 167 | |
hayama | 0:da22b0b4395a | 168 | /** Attach a member function to call whenever a CAN frame received interrupt |
hayama | 0:da22b0b4395a | 169 | * is generated. |
hayama | 0:da22b0b4395a | 170 | * |
hayama | 0:da22b0b4395a | 171 | * @param tptr pointer to the object to call the member function on |
hayama | 0:da22b0b4395a | 172 | * @param mptr pointer to the member function to be called |
hayama | 0:da22b0b4395a | 173 | */ |
hayama | 0:da22b0b4395a | 174 | template<typename T> |
hayama | 0:da22b0b4395a | 175 | void attach(T* tptr, void (T::*mptr)(void)) { |
hayama | 0:da22b0b4395a | 176 | if((mptr != NULL) && (tptr != NULL)) { |
hayama | 0:da22b0b4395a | 177 | _rxirq.attach(tptr, mptr); |
hayama | 0:da22b0b4395a | 178 | setup_interrupt(); |
hayama | 0:da22b0b4395a | 179 | } else { |
hayama | 0:da22b0b4395a | 180 | remove_interrupt(); |
hayama | 0:da22b0b4395a | 181 | } |
hayama | 0:da22b0b4395a | 182 | } |
hayama | 0:da22b0b4395a | 183 | |
hayama | 0:da22b0b4395a | 184 | private: |
hayama | 0:da22b0b4395a | 185 | can_t _can; |
hayama | 0:da22b0b4395a | 186 | FunctionPointer _rxirq; |
hayama | 0:da22b0b4395a | 187 | |
hayama | 0:da22b0b4395a | 188 | void setup_interrupt(void); |
hayama | 0:da22b0b4395a | 189 | void remove_interrupt(void); |
hayama | 0:da22b0b4395a | 190 | }; |
hayama | 0:da22b0b4395a | 191 | |
hayama | 0:da22b0b4395a | 192 | } // namespace mbed |
hayama | 0:da22b0b4395a | 193 | |
hayama | 0:da22b0b4395a | 194 | #endif |
hayama | 0:da22b0b4395a | 195 | |
hayama | 0:da22b0b4395a | 196 | #endif // MBED_CAN_H |