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