RTnoV3 is a program which enables your device to communicate with RT-middleware world This is an example of RTno, which have double type dataport. Actually, in RTno, double is float (IEEE FLOAT32). 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

Committer:
ysuga
Date:
Thu Feb 09 08:14:53 2012 +0000
Revision:
0:422e1f4cc50f
RTno ADC first release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ysuga 0:422e1f4cc50f 1 #include "mbed.h"
ysuga 0:422e1f4cc50f 2
ysuga 0:422e1f4cc50f 3
ysuga 0:422e1f4cc50f 4
ysuga 0:422e1f4cc50f 5 /**
ysuga 0:422e1f4cc50f 6 * RTno_Template.pde
ysuga 0:422e1f4cc50f 7 * RTno is RT-middleware and arduino.
ysuga 0:422e1f4cc50f 8 *
ysuga 0:422e1f4cc50f 9 * Using RTno, arduino device can communicate any RT-components
ysuga 0:422e1f4cc50f 10 * through the RTno-proxy component which is launched in PC.
ysuga 0:422e1f4cc50f 11 * Connect arduino with USB, and program with RTno library.
ysuga 0:422e1f4cc50f 12 * You do not have to define any protocols to establish communication
ysuga 0:422e1f4cc50f 13 * between arduino and PC.
ysuga 0:422e1f4cc50f 14 *
ysuga 0:422e1f4cc50f 15 * Using RTno, you must not define the function "setup" and "loop".
ysuga 0:422e1f4cc50f 16 * Those functions are automatically defined in the RTno libarary.
ysuga 0:422e1f4cc50f 17 * You, developers, must define following functions:
ysuga 0:422e1f4cc50f 18 * int onInitialize(void);
ysuga 0:422e1f4cc50f 19 * int onActivated(void);
ysuga 0:422e1f4cc50f 20 * int onDeactivated(void);
ysuga 0:422e1f4cc50f 21 * int onExecute(void);
ysuga 0:422e1f4cc50f 22 * int onError(void);
ysuga 0:422e1f4cc50f 23 * int onReset(void);
ysuga 0:422e1f4cc50f 24 * These functions are spontaneously called by the RTno-proxy
ysuga 0:422e1f4cc50f 25 * RT-component which is launched in the PC.
ysuga 0:422e1f4cc50f 26 * @author Yuki Suga
ysuga 0:422e1f4cc50f 27 * This code is written/distributed for public-domain.
ysuga 0:422e1f4cc50f 28 */
ysuga 0:422e1f4cc50f 29
ysuga 0:422e1f4cc50f 30 #include <RTno.h>
ysuga 0:422e1f4cc50f 31
ysuga 0:422e1f4cc50f 32 /**
ysuga 0:422e1f4cc50f 33 * This function is called at first.
ysuga 0:422e1f4cc50f 34 * conf._default.baudrate: baudrate of serial communication
ysuga 0:422e1f4cc50f 35 * exec_cxt.periodic.type: reserved but not used.
ysuga 0:422e1f4cc50f 36 */
ysuga 0:422e1f4cc50f 37 void rtcconf(config_str& conf, exec_cxt_str& exec_cxt) {
ysuga 0:422e1f4cc50f 38 // If you want to use Serial Connection, configure below:
ysuga 0:422e1f4cc50f 39 conf._default.connection_type = ConnectionTypeSerialUSB; // USBTX & USBRX (In Windows, Driver must be updated.)
ysuga 0:422e1f4cc50f 40 //conf._default.connection_type = ConnectionTypeSerial1; // pin9=tx, pin10=rx
ysuga 0:422e1f4cc50f 41 //conf._default.connection_type = ConnectionTypeSerial2; // pin13=tx, pin14=rx
ysuga 0:422e1f4cc50f 42 //conf._default.connection_type = ConnectionTypeSerial3; // pin28=tx, pin27=rx
ysuga 0:422e1f4cc50f 43 conf._default.baudrate = 57600; // This value is required when you select ConnectionTypeSerial*
ysuga 0:422e1f4cc50f 44
ysuga 0:422e1f4cc50f 45 // If you want to use EthernetTCP, configure below:
ysuga 0:422e1f4cc50f 46 //conf._default.connection_type = ConnectionTypeEtherTcp;
ysuga 0:422e1f4cc50f 47 //conf._default.port = 23;
ysuga 0:422e1f4cc50f 48 //conf._default.mac_address = MACaddr(0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED);
ysuga 0:422e1f4cc50f 49 //conf._default.ip_address = IPaddr(192,168,42,100);
ysuga 0:422e1f4cc50f 50 //conf._default.subnet_mask = IPaddr(255,255,255,0);
ysuga 0:422e1f4cc50f 51 //conf._default.default_gateway = IPaddr(192,168,42,254);
ysuga 0:422e1f4cc50f 52 // exec_cxt.periodic.type = ProxySynchronousExecutionContext;
ysuga 0:422e1f4cc50f 53 exec_cxt.periodic.type = Timer1ExecutionContext; // onExecute is called by Timer1. Period must be specified by 'rate' option.
ysuga 0:422e1f4cc50f 54 exec_cxt.periodic.rate = 100; // [Hz] This option is indispensable when type is Timer*ExecutionContext.
ysuga 0:422e1f4cc50f 55 }
ysuga 0:422e1f4cc50f 56
ysuga 0:422e1f4cc50f 57
ysuga 0:422e1f4cc50f 58 /**
ysuga 0:422e1f4cc50f 59 * Declaration Division:
ysuga 0:422e1f4cc50f 60 *
ysuga 0:422e1f4cc50f 61 * DataPort and Data Buffer should be placed here.
ysuga 0:422e1f4cc50f 62 *
ysuga 0:422e1f4cc50f 63 * available data types are as follows:
ysuga 0:422e1f4cc50f 64 * TimedLong
ysuga 0:422e1f4cc50f 65 * TimedDouble
ysuga 0:422e1f4cc50f 66 * TimedFloat
ysuga 0:422e1f4cc50f 67 * TimedLongSeq
ysuga 0:422e1f4cc50f 68 * TimedDoubleSeq
ysuga 0:422e1f4cc50f 69 * TimedFloatSeq
ysuga 0:422e1f4cc50f 70 *
ysuga 0:422e1f4cc50f 71 * Please refer following comments. If you need to use some ports,
ysuga 0:422e1f4cc50f 72 * uncomment the line you want to declare.
ysuga 0:422e1f4cc50f 73 **/
ysuga 0:422e1f4cc50f 74 //TimedLong in0;
ysuga 0:422e1f4cc50f 75 //InPort<TimedLong> in0In("in0", in0);
ysuga 0:422e1f4cc50f 76 //TimedLongSeq in0;
ysuga 0:422e1f4cc50f 77 //InPort<TimedLongSeq> in0In("in0", in0);
ysuga 0:422e1f4cc50f 78
ysuga 0:422e1f4cc50f 79 //TimedLong out0;
ysuga 0:422e1f4cc50f 80 //OutPort<TimedLong> out0Out("out0", out0);
ysuga 0:422e1f4cc50f 81 TimedDoubleSeq out0;
ysuga 0:422e1f4cc50f 82 OutPort<TimedDoubleSeq> out0Out("out0", out0);
ysuga 0:422e1f4cc50f 83
ysuga 0:422e1f4cc50f 84
ysuga 0:422e1f4cc50f 85 AnalogIn adcIn0(p15);
ysuga 0:422e1f4cc50f 86 AnalogIn adcIn1(p16);
ysuga 0:422e1f4cc50f 87 AnalogIn adcIn2(p17);
ysuga 0:422e1f4cc50f 88 AnalogIn adcIn3(p18);
ysuga 0:422e1f4cc50f 89 AnalogIn adcIn4(p19);
ysuga 0:422e1f4cc50f 90 AnalogIn adcIn5(p20);
ysuga 0:422e1f4cc50f 91
ysuga 0:422e1f4cc50f 92 //////////////////////////////////////////
ysuga 0:422e1f4cc50f 93 // on_initialize
ysuga 0:422e1f4cc50f 94 //
ysuga 0:422e1f4cc50f 95 // This function is called in the initialization
ysuga 0:422e1f4cc50f 96 // sequence. The sequence is triggered by the
ysuga 0:422e1f4cc50f 97 // PC. When the RTnoRTC is launched in the PC,
ysuga 0:422e1f4cc50f 98 // then, this function is remotely called
ysuga 0:422e1f4cc50f 99 // through the USB cable.
ysuga 0:422e1f4cc50f 100 // In on_initialize, usually DataPorts are added.
ysuga 0:422e1f4cc50f 101 //
ysuga 0:422e1f4cc50f 102 //////////////////////////////////////////
ysuga 0:422e1f4cc50f 103 int RTno::onInitialize() {
ysuga 0:422e1f4cc50f 104 /* Data Ports are added in this section.
ysuga 0:422e1f4cc50f 105 * addInPort(in1In);
ysuga 0:422e1f4cc50f 106 *
ysuga 0:422e1f4cc50f 107 * addOutPort(out1Out);
ysuga 0:422e1f4cc50f 108 */
ysuga 0:422e1f4cc50f 109 addOutPort(out0Out);
ysuga 0:422e1f4cc50f 110
ysuga 0:422e1f4cc50f 111 // Some initialization (like port direction setting)
ysuga 0:422e1f4cc50f 112 // int LED = 13;
ysuga 0:422e1f4cc50f 113 return RTC_OK;
ysuga 0:422e1f4cc50f 114 }
ysuga 0:422e1f4cc50f 115
ysuga 0:422e1f4cc50f 116 ////////////////////////////////////////////
ysuga 0:422e1f4cc50f 117 // on_activated
ysuga 0:422e1f4cc50f 118 // This function is called when the RTnoRTC
ysuga 0:422e1f4cc50f 119 // is activated. When the activation, the RTnoRTC
ysuga 0:422e1f4cc50f 120 // sends message to call this function remotely.
ysuga 0:422e1f4cc50f 121 // If this function is failed (return value
ysuga 0:422e1f4cc50f 122 // is RTC_ERROR), RTno will enter ERROR condition.
ysuga 0:422e1f4cc50f 123 ////////////////////////////////////////////
ysuga 0:422e1f4cc50f 124 int RTno::onActivated() {
ysuga 0:422e1f4cc50f 125 // Write here initialization code.
ysuga 0:422e1f4cc50f 126 return RTC_OK;
ysuga 0:422e1f4cc50f 127 }
ysuga 0:422e1f4cc50f 128
ysuga 0:422e1f4cc50f 129 /////////////////////////////////////////////
ysuga 0:422e1f4cc50f 130 // on_deactivated
ysuga 0:422e1f4cc50f 131 // This function is called when the RTnoRTC
ysuga 0:422e1f4cc50f 132 // is deactivated.
ysuga 0:422e1f4cc50f 133 /////////////////////////////////////////////
ysuga 0:422e1f4cc50f 134 int RTno::onDeactivated()
ysuga 0:422e1f4cc50f 135 {
ysuga 0:422e1f4cc50f 136 // Write here finalization code.
ysuga 0:422e1f4cc50f 137 return RTC_OK;
ysuga 0:422e1f4cc50f 138 }
ysuga 0:422e1f4cc50f 139
ysuga 0:422e1f4cc50f 140 //////////////////////////////////////////////
ysuga 0:422e1f4cc50f 141 // This function is repeatedly called when the
ysuga 0:422e1f4cc50f 142 // RTno is in the ACTIVE condition.
ysuga 0:422e1f4cc50f 143 // If this function is failed (return value is
ysuga 0:422e1f4cc50f 144 // RTC_ERROR), RTno immediately enter into the
ysuga 0:422e1f4cc50f 145 // ERROR condition.r
ysuga 0:422e1f4cc50f 146 //////////////////////////////////////////////
ysuga 0:422e1f4cc50f 147 int RTno::onExecute() {
ysuga 0:422e1f4cc50f 148 /**
ysuga 0:422e1f4cc50f 149 * Usage of InPort with premitive type.
ysuga 0:422e1f4cc50f 150 */
ysuga 0:422e1f4cc50f 151 /*
ysuga 0:422e1f4cc50f 152 if(in0In.isNew()) {
ysuga 0:422e1f4cc50f 153 in0In.read();
ysuga 0:422e1f4cc50f 154 led = in0.data;
ysuga 0:422e1f4cc50f 155 }
ysuga 0:422e1f4cc50f 156 */
ysuga 0:422e1f4cc50f 157
ysuga 0:422e1f4cc50f 158
ysuga 0:422e1f4cc50f 159 /**
ysuga 0:422e1f4cc50f 160 * Usage of InPort with sequence type
ysuga 0:422e1f4cc50f 161 */
ysuga 0:422e1f4cc50f 162 /*
ysuga 0:422e1f4cc50f 163 if(in0In.isNew(&in1In)) {
ysuga 0:422e1f4cc50f 164 in0In.read();
ysuga 0:422e1f4cc50f 165 for(int i = 0;i < in0.data.length;i++) {
ysuga 0:422e1f4cc50f 166 long data_buffer = in0.data[i];
ysuga 0:422e1f4cc50f 167 }
ysuga 0:422e1f4cc50f 168 }
ysuga 0:422e1f4cc50f 169 */
ysuga 0:422e1f4cc50f 170
ysuga 0:422e1f4cc50f 171 /**
ysuga 0:422e1f4cc50f 172 * Usage of OutPort with primitive type.
ysuga 0:422e1f4cc50f 173 out0.data = 3.14159;
ysuga 0:422e1f4cc50f 174 out0Out.write();
ysuga 0:422e1f4cc50f 175 */
ysuga 0:422e1f4cc50f 176
ysuga 0:422e1f4cc50f 177 /**
ysuga 0:422e1f4cc50f 178 * Usage of OutPort with sequence type.
ysuga 0:422e1f4cc50f 179 */
ysuga 0:422e1f4cc50f 180 out0.data.length(6);
ysuga 0:422e1f4cc50f 181 out0.data[0] = (float)(adcIn0.read_u16() >> 4) * 3.0 / 1024;
ysuga 0:422e1f4cc50f 182 out0.data[1] = (float)(adcIn1.read_u16() >> 4) * 3.0 / 1024;
ysuga 0:422e1f4cc50f 183 out0.data[2] = (float)(adcIn2.read_u16() >> 4) * 3.0 / 1024;
ysuga 0:422e1f4cc50f 184 out0.data[3] = (float)(adcIn3.read_u16() >> 4) * 3.0 / 1024;
ysuga 0:422e1f4cc50f 185 out0.data[4] = (float)(adcIn4.read_u16() >> 4) * 3.0 / 1024;
ysuga 0:422e1f4cc50f 186 out0.data[5] = (float)(adcIn5.read_u16() >> 4) * 3.0 / 1024;
ysuga 0:422e1f4cc50f 187 out0Out.write();
ysuga 0:422e1f4cc50f 188
ysuga 0:422e1f4cc50f 189
ysuga 0:422e1f4cc50f 190 return RTC_OK;
ysuga 0:422e1f4cc50f 191 }
ysuga 0:422e1f4cc50f 192
ysuga 0:422e1f4cc50f 193
ysuga 0:422e1f4cc50f 194 //////////////////////////////////////
ysuga 0:422e1f4cc50f 195 // on_error
ysuga 0:422e1f4cc50f 196 // This function is repeatedly called when
ysuga 0:422e1f4cc50f 197 // the RTno is in the ERROR condition.
ysuga 0:422e1f4cc50f 198 // The ERROR condition can be recovered,
ysuga 0:422e1f4cc50f 199 // when the RTno is reset.
ysuga 0:422e1f4cc50f 200 ///////////////////////////////////////
ysuga 0:422e1f4cc50f 201 int RTno::onError()
ysuga 0:422e1f4cc50f 202 {
ysuga 0:422e1f4cc50f 203 return RTC_OK;
ysuga 0:422e1f4cc50f 204 }
ysuga 0:422e1f4cc50f 205
ysuga 0:422e1f4cc50f 206 ////////////////////////////////////////
ysuga 0:422e1f4cc50f 207 // This function is called when
ysuga 0:422e1f4cc50f 208 // the RTno is reset. If on_reset is
ysuga 0:422e1f4cc50f 209 // succeeded, the RTno will enter into
ysuga 0:422e1f4cc50f 210 // the INACTIVE condition. If failed
ysuga 0:422e1f4cc50f 211 // (return value is RTC_ERROR), RTno
ysuga 0:422e1f4cc50f 212 // will stay in ERROR condition.ec
ysuga 0:422e1f4cc50f 213 ///////////////////////////////////////
ysuga 0:422e1f4cc50f 214 int RTno::onReset()
ysuga 0:422e1f4cc50f 215 {
ysuga 0:422e1f4cc50f 216 return RTC_OK;
ysuga 0:422e1f4cc50f 217 }