Go to the documentation of this file.00001 #include <fcntl.h>
00002 #include <stdio.h>
00003 #include <unistd.h>
00004 #include <sys/ioctl.h>
00005 #include <sys/types.h>
00006 #include <sys/stat.h>
00007
00008 #include <signal.h>
00009
00010 #include "driver/urbtc.h"
00011 #include "driver/urobotc.h"
00012
00013 #undef __BIG_ENDIAN
00014
00015 void exit_program();
00016
00017 int quit_flag = 1;
00018 int fd;
00019
00020 int main(int argc, char **argv)
00021 {
00022 struct uin buf;
00023 struct uout obuf;
00024 struct ccmd cmd;
00025 int i;
00026
00027 char *dev = "/dev/urbtc0";
00028
00029 signal(SIGINT, exit_program);
00030
00031 if (argc>1)
00032 dev = argv[1];
00033
00034 if ((fd = open(dev, O_RDWR)) == -1) {
00035 fprintf(stderr, "%s: Open error\n", dev);
00036 exit(1);
00037 }
00038
00039 cmd.retval = 0 ;
00040 cmd.setoffset = CH0 | CH1 | CH2 | CH3;
00041 cmd.setcounter = CH0 | CH1 | CH2 | CH3;
00042 cmd.resetint = CH0 | CH1 | CH2 | CH3;
00043
00044 cmd.selin = CH0 | CH1 | CH2 | CH3 | SET_SELECT;
00045
00046 cmd.dout = 0;
00047
00048 cmd.selout = SET_SELECT | CH0 | CH1 | CH2 | CH3;
00049
00050
00051 cmd.offset[0] = cmd.offset[1] = cmd.offset[2] = cmd.offset[3] = 32768;
00052
00053 cmd.counter[0] = cmd.counter[1] = cmd.counter[2] = cmd.counter[3] = 0;
00054
00055 cmd.posneg = SET_POSNEG | CH0 | CH1 | CH2 | CH3;
00056 cmd.breaks = SET_BREAKS | CH0 | CH1 | CH2 | CH3;
00057
00058 cmd.magicno = 0x00;
00059
00060 cmd.wrrom = 0;
00061
00062
00063 if (ioctl(fd, URBTC_COUNTER_SET) < 0){
00064 fprintf(stderr, "ioctl: URBTC_COUNTER_SET error\n");
00065 exit(1);
00066 }
00067
00068
00069 if (write(fd, &cmd, sizeof(cmd)) < 0) {
00070 fprintf(stderr, "write error\n");
00071 exit(1);
00072 }
00073
00074
00075
00076 if (ioctl(fd, URBTC_DESIRE_SET) < 0){
00077 fprintf(stderr, "ioctl: URBTC_DESIRE_SET error\n");
00078 exit(1);
00079 }
00080
00081
00082 for (i=0; i<4; i++) {
00083 obuf.ch[i].x = 0;
00084 obuf.ch[i].d = 0;
00085 obuf.ch[i].kp = 0x2;
00086 obuf.ch[i].kpx = 1;
00087 obuf.ch[i].kd = 0;
00088 obuf.ch[i].kdx = 1;
00089 obuf.ch[i].ki = 0;
00090 obuf.ch[i].kix = 1;
00091 }
00092
00093
00094 if (ioctl(fd, URBTC_CONTINUOUS_READ) < 0){
00095 fprintf(stderr, "ioctl: URBTC_CONTINUOUS_READ error\n");
00096 exit(1);
00097 }
00098
00099 if (ioctl(fd, URBTC_BUFREAD) < 0){
00100 fprintf(stderr, "ioctl: URBTC_CONTINUOUS_READ error\n");
00101 exit(1);
00102 }
00103
00104 while(quit_flag) {
00105
00106
00107 unsigned short x_d= 511.0;
00108
00109
00110 if ((i = read(fd, &buf, sizeof(buf))) != sizeof(buf)) {
00111 fprintf(stderr, "Warning: read size mismatch (%d!=%d).\n", i, sizeof(buf));
00112 continue;
00113 }
00114
00115 for (i=0; i<4; i++) {
00116 buf.ad[i] >>= 5;
00117 }
00118
00119 printf("current=%d, delta=%d\n", buf.ad[3], x_d - buf.ad[3]);
00120
00121 x_d <<= 5;
00122
00123
00124 obuf.ch[0].x = obuf.ch[1].x = obuf.ch[2].x = obuf.ch[3].x = x_d;
00125
00126
00127 if (write(fd, &obuf, sizeof(obuf)) > 0) {
00128 i++;
00129 } else {
00130 printf("write err\n");
00131 break;
00132 }
00133 }
00134
00135 close(fd);
00136
00137 return 0;
00138 }
00139
00140 void exit_program(sig, code, scp, addr)
00141 int sig;
00142 int code;
00143 struct sigcontext *scp;
00144 char *addr;
00145 {
00146 quit_flag = 0;
00147 fprintf(stderr, "kill signal is received\n");
00148 }