RTnoV3 is a program which enables your device to communicate with RT-middleware world This is an example for using Timer1ExecutionContext To know RT-middleware, visit: http://www.openrtm.org To know more about RTno, visit: http://ysuga.net/robot_e/rtm_e/rtc_e/1065?lang=en
Dependencies: EthernetNetIf mbed RTnoV3
main.cpp@0:626a5fcaa2ec, 2012-02-09 (annotated)
- Committer:
- ysuga
- Date:
- Thu Feb 09 09:44:29 2012 +0000
- Revision:
- 0:626a5fcaa2ec
RTno V3 Timer first publish
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ysuga | 0:626a5fcaa2ec | 1 | #include "mbed.h" |
ysuga | 0:626a5fcaa2ec | 2 | |
ysuga | 0:626a5fcaa2ec | 3 | |
ysuga | 0:626a5fcaa2ec | 4 | |
ysuga | 0:626a5fcaa2ec | 5 | /** |
ysuga | 0:626a5fcaa2ec | 6 | * RTno_Template.pde |
ysuga | 0:626a5fcaa2ec | 7 | * RTno is RT-middleware and arduino. |
ysuga | 0:626a5fcaa2ec | 8 | * |
ysuga | 0:626a5fcaa2ec | 9 | * Using RTno, arduino device can communicate any RT-components |
ysuga | 0:626a5fcaa2ec | 10 | * through the RTno-proxy component which is launched in PC. |
ysuga | 0:626a5fcaa2ec | 11 | * Connect arduino with USB, and program with RTno library. |
ysuga | 0:626a5fcaa2ec | 12 | * You do not have to define any protocols to establish communication |
ysuga | 0:626a5fcaa2ec | 13 | * between arduino and PC. |
ysuga | 0:626a5fcaa2ec | 14 | * |
ysuga | 0:626a5fcaa2ec | 15 | * Using RTno, you must not define the function "setup" and "loop". |
ysuga | 0:626a5fcaa2ec | 16 | * Those functions are automatically defined in the RTno libarary. |
ysuga | 0:626a5fcaa2ec | 17 | * You, developers, must define following functions: |
ysuga | 0:626a5fcaa2ec | 18 | * int onInitialize(void); |
ysuga | 0:626a5fcaa2ec | 19 | * int onActivated(void); |
ysuga | 0:626a5fcaa2ec | 20 | * int onDeactivated(void); |
ysuga | 0:626a5fcaa2ec | 21 | * int onExecute(void); |
ysuga | 0:626a5fcaa2ec | 22 | * int onError(void); |
ysuga | 0:626a5fcaa2ec | 23 | * int onReset(void); |
ysuga | 0:626a5fcaa2ec | 24 | * These functions are spontaneously called by the RTno-proxy |
ysuga | 0:626a5fcaa2ec | 25 | * RT-component which is launched in the PC. |
ysuga | 0:626a5fcaa2ec | 26 | * @author Yuki Suga |
ysuga | 0:626a5fcaa2ec | 27 | * This code is written/distributed for public-domain. |
ysuga | 0:626a5fcaa2ec | 28 | */ |
ysuga | 0:626a5fcaa2ec | 29 | |
ysuga | 0:626a5fcaa2ec | 30 | #include <RTno.h> |
ysuga | 0:626a5fcaa2ec | 31 | |
ysuga | 0:626a5fcaa2ec | 32 | /** |
ysuga | 0:626a5fcaa2ec | 33 | * This function is called at first. |
ysuga | 0:626a5fcaa2ec | 34 | * conf._default.baudrate: baudrate of serial communication |
ysuga | 0:626a5fcaa2ec | 35 | * exec_cxt.periodic.type: reserved but not used. |
ysuga | 0:626a5fcaa2ec | 36 | */ |
ysuga | 0:626a5fcaa2ec | 37 | void rtcconf(config_str& conf, exec_cxt_str& exec_cxt) { |
ysuga | 0:626a5fcaa2ec | 38 | // If you want to use Serial Connection, configure below: |
ysuga | 0:626a5fcaa2ec | 39 | conf._default.connection_type = ConnectionTypeSerialUSB; // USBTX & USBRX (In Windows, Driver must be updated.) |
ysuga | 0:626a5fcaa2ec | 40 | conf._default.baudrate = 57600; // This value is required when you select ConnectionTypeSerial* |
ysuga | 0:626a5fcaa2ec | 41 | |
ysuga | 0:626a5fcaa2ec | 42 | exec_cxt.periodic.type = Timer1ExecutionContext; // onExecute is called by Timer1. Period must be specified by 'rate' option. |
ysuga | 0:626a5fcaa2ec | 43 | exec_cxt.periodic.rate = 1; // [Hz] This option is indispensable when type is Timer*ExecutionContext. |
ysuga | 0:626a5fcaa2ec | 44 | } |
ysuga | 0:626a5fcaa2ec | 45 | |
ysuga | 0:626a5fcaa2ec | 46 | |
ysuga | 0:626a5fcaa2ec | 47 | /** |
ysuga | 0:626a5fcaa2ec | 48 | * Declaration Division: |
ysuga | 0:626a5fcaa2ec | 49 | * |
ysuga | 0:626a5fcaa2ec | 50 | * DataPort and Data Buffer should be placed here. |
ysuga | 0:626a5fcaa2ec | 51 | * |
ysuga | 0:626a5fcaa2ec | 52 | * available data types are as follows: |
ysuga | 0:626a5fcaa2ec | 53 | * TimedLong |
ysuga | 0:626a5fcaa2ec | 54 | * TimedDouble |
ysuga | 0:626a5fcaa2ec | 55 | * TimedFloat |
ysuga | 0:626a5fcaa2ec | 56 | * TimedLongSeq |
ysuga | 0:626a5fcaa2ec | 57 | * TimedDoubleSeq |
ysuga | 0:626a5fcaa2ec | 58 | * TimedFloatSeq |
ysuga | 0:626a5fcaa2ec | 59 | * |
ysuga | 0:626a5fcaa2ec | 60 | * Please refer following comments. If you need to use some ports, |
ysuga | 0:626a5fcaa2ec | 61 | * uncomment the line you want to declare. |
ysuga | 0:626a5fcaa2ec | 62 | **/ |
ysuga | 0:626a5fcaa2ec | 63 | |
ysuga | 0:626a5fcaa2ec | 64 | |
ysuga | 0:626a5fcaa2ec | 65 | DigitalOut led1(LED1); |
ysuga | 0:626a5fcaa2ec | 66 | DigitalOut led2(LED2); |
ysuga | 0:626a5fcaa2ec | 67 | DigitalOut led3(LED3); |
ysuga | 0:626a5fcaa2ec | 68 | DigitalOut led4(LED4); |
ysuga | 0:626a5fcaa2ec | 69 | |
ysuga | 0:626a5fcaa2ec | 70 | ////////////////////////////////////////// |
ysuga | 0:626a5fcaa2ec | 71 | // on_initialize |
ysuga | 0:626a5fcaa2ec | 72 | // |
ysuga | 0:626a5fcaa2ec | 73 | // This function is called in the initialization |
ysuga | 0:626a5fcaa2ec | 74 | // sequence. The sequence is triggered by the |
ysuga | 0:626a5fcaa2ec | 75 | // PC. When the RTnoRTC is launched in the PC, |
ysuga | 0:626a5fcaa2ec | 76 | // then, this function is remotely called |
ysuga | 0:626a5fcaa2ec | 77 | // through the USB cable. |
ysuga | 0:626a5fcaa2ec | 78 | // In on_initialize, usually DataPorts are added. |
ysuga | 0:626a5fcaa2ec | 79 | // |
ysuga | 0:626a5fcaa2ec | 80 | ////////////////////////////////////////// |
ysuga | 0:626a5fcaa2ec | 81 | int RTno::onInitialize() { |
ysuga | 0:626a5fcaa2ec | 82 | /* Data Ports are added in this section. |
ysuga | 0:626a5fcaa2ec | 83 | */ |
ysuga | 0:626a5fcaa2ec | 84 | |
ysuga | 0:626a5fcaa2ec | 85 | addInPort(in0In); |
ysuga | 0:626a5fcaa2ec | 86 | |
ysuga | 0:626a5fcaa2ec | 87 | // Some initialization (like port direction setting) |
ysuga | 0:626a5fcaa2ec | 88 | led1 = 1; |
ysuga | 0:626a5fcaa2ec | 89 | led2 = 0; |
ysuga | 0:626a5fcaa2ec | 90 | led3 = 0; |
ysuga | 0:626a5fcaa2ec | 91 | led4 = 0; |
ysuga | 0:626a5fcaa2ec | 92 | return RTC_OK; |
ysuga | 0:626a5fcaa2ec | 93 | } |
ysuga | 0:626a5fcaa2ec | 94 | |
ysuga | 0:626a5fcaa2ec | 95 | //////////////////////////////////////////// |
ysuga | 0:626a5fcaa2ec | 96 | // on_activated |
ysuga | 0:626a5fcaa2ec | 97 | // This function is called when the RTnoRTC |
ysuga | 0:626a5fcaa2ec | 98 | // is activated. When the activation, the RTnoRTC |
ysuga | 0:626a5fcaa2ec | 99 | // sends message to call this function remotely. |
ysuga | 0:626a5fcaa2ec | 100 | // If this function is failed (return value |
ysuga | 0:626a5fcaa2ec | 101 | // is RTC_ERROR), RTno will enter ERROR condition. |
ysuga | 0:626a5fcaa2ec | 102 | //////////////////////////////////////////// |
ysuga | 0:626a5fcaa2ec | 103 | int RTno::onActivated() { |
ysuga | 0:626a5fcaa2ec | 104 | // Write here initialization code. |
ysuga | 0:626a5fcaa2ec | 105 | led2 = 1; |
ysuga | 0:626a5fcaa2ec | 106 | led3 = 0; |
ysuga | 0:626a5fcaa2ec | 107 | led4 = 0; |
ysuga | 0:626a5fcaa2ec | 108 | return RTC_OK; |
ysuga | 0:626a5fcaa2ec | 109 | } |
ysuga | 0:626a5fcaa2ec | 110 | |
ysuga | 0:626a5fcaa2ec | 111 | ///////////////////////////////////////////// |
ysuga | 0:626a5fcaa2ec | 112 | // on_deactivated |
ysuga | 0:626a5fcaa2ec | 113 | // This function is called when the RTnoRTC |
ysuga | 0:626a5fcaa2ec | 114 | // is deactivated. |
ysuga | 0:626a5fcaa2ec | 115 | ///////////////////////////////////////////// |
ysuga | 0:626a5fcaa2ec | 116 | int RTno::onDeactivated() |
ysuga | 0:626a5fcaa2ec | 117 | { |
ysuga | 0:626a5fcaa2ec | 118 | // Write here finalization code. |
ysuga | 0:626a5fcaa2ec | 119 | led2 = 0; |
ysuga | 0:626a5fcaa2ec | 120 | led3 = 0; |
ysuga | 0:626a5fcaa2ec | 121 | led4 = 0; |
ysuga | 0:626a5fcaa2ec | 122 | return RTC_OK; |
ysuga | 0:626a5fcaa2ec | 123 | } |
ysuga | 0:626a5fcaa2ec | 124 | |
ysuga | 0:626a5fcaa2ec | 125 | ////////////////////////////////////////////// |
ysuga | 0:626a5fcaa2ec | 126 | // This function is repeatedly called when the |
ysuga | 0:626a5fcaa2ec | 127 | // RTno is in the ACTIVE condition. |
ysuga | 0:626a5fcaa2ec | 128 | // If this function is failed (return value is |
ysuga | 0:626a5fcaa2ec | 129 | // RTC_ERROR), RTno immediately enter into the |
ysuga | 0:626a5fcaa2ec | 130 | // ERROR condition.r |
ysuga | 0:626a5fcaa2ec | 131 | ////////////////////////////////////////////// |
ysuga | 0:626a5fcaa2ec | 132 | int RTno::onExecute() { |
ysuga | 0:626a5fcaa2ec | 133 | static int flag; |
ysuga | 0:626a5fcaa2ec | 134 | if(flag) { |
ysuga | 0:626a5fcaa2ec | 135 | led3 = 1; |
ysuga | 0:626a5fcaa2ec | 136 | flag = 0; |
ysuga | 0:626a5fcaa2ec | 137 | } else { |
ysuga | 0:626a5fcaa2ec | 138 | led3 = 0; |
ysuga | 0:626a5fcaa2ec | 139 | flag++; |
ysuga | 0:626a5fcaa2ec | 140 | } |
ysuga | 0:626a5fcaa2ec | 141 | |
ysuga | 0:626a5fcaa2ec | 142 | return RTC_OK; |
ysuga | 0:626a5fcaa2ec | 143 | } |
ysuga | 0:626a5fcaa2ec | 144 | |
ysuga | 0:626a5fcaa2ec | 145 | |
ysuga | 0:626a5fcaa2ec | 146 | ////////////////////////////////////// |
ysuga | 0:626a5fcaa2ec | 147 | // on_error |
ysuga | 0:626a5fcaa2ec | 148 | // This function is repeatedly called when |
ysuga | 0:626a5fcaa2ec | 149 | // the RTno is in the ERROR condition. |
ysuga | 0:626a5fcaa2ec | 150 | // The ERROR condition can be recovered, |
ysuga | 0:626a5fcaa2ec | 151 | // when the RTno is reset. |
ysuga | 0:626a5fcaa2ec | 152 | /////////////////////////////////////// |
ysuga | 0:626a5fcaa2ec | 153 | int RTno::onError() |
ysuga | 0:626a5fcaa2ec | 154 | { |
ysuga | 0:626a5fcaa2ec | 155 | return RTC_OK; |
ysuga | 0:626a5fcaa2ec | 156 | } |
ysuga | 0:626a5fcaa2ec | 157 | |
ysuga | 0:626a5fcaa2ec | 158 | //////////////////////////////////////// |
ysuga | 0:626a5fcaa2ec | 159 | // This function is called when |
ysuga | 0:626a5fcaa2ec | 160 | // the RTno is reset. If on_reset is |
ysuga | 0:626a5fcaa2ec | 161 | // succeeded, the RTno will enter into |
ysuga | 0:626a5fcaa2ec | 162 | // the INACTIVE condition. If failed |
ysuga | 0:626a5fcaa2ec | 163 | // (return value is RTC_ERROR), RTno |
ysuga | 0:626a5fcaa2ec | 164 | // will stay in ERROR condition.ec |
ysuga | 0:626a5fcaa2ec | 165 | /////////////////////////////////////// |
ysuga | 0:626a5fcaa2ec | 166 | int RTno::onReset() |
ysuga | 0:626a5fcaa2ec | 167 | { |
ysuga | 0:626a5fcaa2ec | 168 | return RTC_OK; |
ysuga | 0:626a5fcaa2ec | 169 | } |