urobot_1ch_enc_pos.c
Go to the documentation of this file.
00001 #include <fcntl.h>          /* for open */
00002 #include <stdio.h>
00003 #include <unistd.h>         /* for read, close */
00004 #include <sys/ioctl.h>      /* for ioctl */
00005 #include <sys/types.h>      /* for open */
00006 #include <sys/stat.h>       /* for open */
00007 
00008 #include <signal.h>
00009 
00010 #include "driver/urbtc.h"          /* Linux specific part */
00011 #include "driver/urobotc.h"        /* OS independent part */
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,j=0;
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 /* RETURN_VAL */;
00040   cmd.setoffset  = CH0 | CH1 | CH2 | CH3;
00041   cmd.setcounter = CH0 | CH1 | CH2 | CH3;
00042   cmd.resetint   = CH0 | CH1 | CH2 | CH3;
00043 
00044   /*CH0 をエンコーダ入力, CH2を CH0 のエンコーダ入力に設定*/
00045   cmd.selin = SET_SELECT | SET_CH2_HIN;
00046   cmd.dout = 0;
00047 
00048   /*CH0をDA出力,CH2をPWM出力に設定*/
00049   cmd.selout = SET_SELECT | CH2;
00050 
00051   cmd.offset[0] = cmd.offset[1] = cmd.offset[2] = cmd.offset[3] = 32768;
00052   cmd.counter[0] = cmd.counter[1] = cmd.counter[2] = cmd.counter[3] = 0;
00053   cmd.posneg = SET_POSNEG | CH0| CH1 | CH2 | CH3; /*POS PWM out*/
00054   cmd.breaks = SET_BREAKS | CH0 | CH1 | CH2 | CH3; /*No Brake*/
00055   cmd.magicno = 0x00;
00056 
00057   cmd.wrrom = 0; /* WR_MAGIC | WR_OFFSET | WR_SELOUT;*/
00058 
00059   if (ioctl(fd, URBTC_COUNTER_SET) < 0){
00060     fprintf(stderr, "ioctl: URBTC_COUNTER_SET error\n");
00061     exit(1);
00062   }
00063 
00064   if (write(fd, &cmd, sizeof(cmd)) < 0) {
00065     fprintf(stderr, "write error\n");
00066     exit(1);
00067   }
00068 
00069   for (i=0; i<4; i++) {
00070     obuf.ch[i].x = 0;
00071     obuf.ch[i].d = 0;
00072     obuf.ch[i].kp = 10;
00073     obuf.ch[i].kpx = 1;
00074     obuf.ch[i].kd = 0;
00075     obuf.ch[i].kdx = 1;
00076     obuf.ch[i].ki = 0;
00077     obuf.ch[i].kix = 1;
00078   }
00079 
00080   /*set scmd mode*/
00081   if (ioctl(fd, URBTC_DESIRE_SET) < 0){
00082     fprintf(stderr, "ioctl: URBTC_DESIRE_SET error\n");
00083     exit(1);
00084   }
00085 
00086   if (ioctl(fd, URBTC_CONTINUOUS_READ) < 0){
00087     fprintf(stderr, "ioctl: URBTC_CONTINUOUS_READ error\n");
00088     exit(1);
00089   }
00090 
00091   if (ioctl(fd, URBTC_BUFREAD) < 0){
00092     fprintf(stderr, "ioctl: URBTC_CONTINUOUS_READ error\n");
00093     exit(1);
00094   }
00095 
00096   while(quit_flag) {
00097 
00098     unsigned short x_d= 100 * sin(j*3.14/655.360);
00099     x_d <<= 5;
00100 
00101     /*CH2の制御ループを用いる*/
00102     obuf.ch[2].x = x_d;
00103 
00104     if (write(fd, &obuf, sizeof(obuf)) > 0) {
00105       j++;
00106       //printf("OK\n");
00107     } else {
00108       printf("write err\n");
00109       break;
00110     }
00111   }
00112 
00113   close(fd);
00114 
00115   return 0;
00116 }
00117 
00118 void exit_program(sig, code, scp, addr)
00119 int sig;
00120 int code;
00121 struct sigcontext *scp;
00122 char *addr;
00123 {
00124   quit_flag = 0;
00125   fprintf(stderr, "kill signal is received\n");
00126 }


cirkit_unit03_driver
Author(s): CIR-KIT
autogenerated on Thu Jun 6 2019 21:08:20