home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / robotics / 1757 < prev    next >
Encoding:
Text File  |  1992-09-10  |  4.6 KB  |  172 lines

  1. Newsgroups: comp.robotics
  2. Path: sparky!uunet!spool.mu.edu!umn.edu!lynx!nmsu.edu!opus!ted
  3. From: ted@nmsu.edu (Ted Dunning)
  4. Subject: Re: Stepper motor programs wanted
  5. In-Reply-To: pvr@wang.com's message of 9 Sep 92 18:15:42 GMT
  6. Message-ID: <TED.92Sep10103428@lole.nmsu.edu>
  7. Sender: usenet@nmsu.edu
  8. Reply-To: ted@nmsu.edu
  9. Organization: Computing Research Lab
  10. References: <gate.LJuLqB1w165w@toz.buffalo.ny.us> <1992Sep8.202500.27325@oswego.Oswego.EDU>
  11.     <bubpe7.1wt@wang.com>
  12. Date: Thu, 10 Sep 1992 17:34:28 GMT
  13. Lines: 157
  14.  
  15.  
  16.  
  17. since i have had some requests for my multi-axis stepper motor code,
  18. and since i deleted the original code years ago, i wrote this small
  19. program this morning.  hope it helps.  hope nobody minds code in this
  20. group.  (it _is_ short).  send me any comments.
  21.  
  22. /*   copyright (c) 1992 ted dunning  (ted@nmsu.edu)
  23.  
  24. this code may be duplicated and modified for non-commercial use.
  25.  
  26. write and ask for permission for duplication and modification for
  27. commercial use.
  28.  
  29.    coordinate multiple stepping in many axes.  uses a variation on
  30.    bresenham's algorithm adapted for multiple dimensions
  31.  
  32. for the demo version the number of steps for each axis are given on
  33. the command line and the output consists of one line of output for
  34. each step.  a simple change in the code can give slightly different
  35. results that might be considered more asthetic by some.
  36.  
  37. for example (my additions marked by #):
  38.  
  39. > motor 1 2 5            # without mid step mods
  40. stepping 0            # first step for 0
  41. stepping 1            # and 1
  42. stepping 2            # and 2
  43.  
  44. stepping 2            # second step for 2
  45.  
  46. stepping 1            # second step for 1
  47. stepping 2            # third for 2
  48.  
  49. stepping 2            # fourth for 2
  50.  
  51. stepping 2            # and fifth for 2.
  52.  
  53. > motor 1 2 5            # with mid step mods
  54. stepping 2            # step 1 for 2
  55.  
  56. stepping 1            # step 1 for 1
  57. stepping 2            # step 2 for 2
  58.  
  59. stepping 0            # step 1 for 0
  60. stepping 2            # step 3 for 2
  61.  
  62. stepping 1            # step 2 for 1
  63. stepping 2            # step 4 for 2
  64.  
  65. stepping 2            # step 5 for 2
  66.  
  67. it may help to define a dummy motor if you want to space the
  68. steps out in time.  stepping the dummy motor would only involve
  69. waiting for the next time tick.
  70.  
  71. */
  72.  
  73. #include <stdio.h>
  74. #include <assert.h>
  75. #include <malloc.h>
  76.  
  77. static int array_max(int x[], int n)
  78. {
  79.     int max, i;
  80.  
  81.     assert(n>0);
  82.     max = x[0];
  83.     for (i=1;i<n;i++) {
  84.     if (max<x[i]) max = x[i];
  85.     }
  86.     return max;
  87. }
  88.  
  89. /* each motors count will be decremented by dx[i] each of biggest times
  90.    through the main loop.  each time it goes negative, it will be
  91.    incremented by biggest.  at the end, each will have been decremented
  92.    by a total of biggest * dx[i], and since the test comes after the
  93.    decrement, it will have be >= 0 after the loop.  since it will have
  94.    been incremented by biggest the smallest number of times, it will,
  95.    in fact, have been incremented dx[i] times for a total of dx[i] * biggest.
  96.    thus, each count will be = 0 at the end.  furthermore... it is pretty
  97.    clear that the increment operations will be spaced out pretty much
  98.    as nicely as possible.
  99.  
  100.    one problem with this code is that every motor takes a step on the first
  101.    tick.  this leads to unsavory effects in graphics programs, but i think
  102.    it might be good for motors.  if you don't like it, then double the
  103.    increments and decrements and start each count at biggest.  remember to
  104.    change the test at the end to check for biggest instead of zero.
  105.    */
  106.  
  107. void coordinate(int dx[], int n, void (*stepper)(int))
  108. {
  109.     int biggest;
  110.     static int oldn=0;
  111.     static int *count=NULL;
  112.     int step, i;
  113.  
  114.     if (oldn < n) {
  115.     if (count) free(count);
  116.     count = calloc(n, sizeof(count[0]));
  117.     assert(count);
  118.  
  119.     oldn = n;
  120.     }
  121.  
  122.     biggest = array_max(dx, n);
  123.     for (i=0;i<n;i++) {
  124.     count[i] = 0;        /* = biggest for mid step version*/
  125.     }
  126.  
  127.     for (step=0;step<biggest;step++) {
  128.     /* check each motor for possible step */
  129.     for (i=0;i<n;i++) {
  130.         count[i] -= dx[i];    /* -= 2*dx[i] for mid step version */
  131.         if (count[i] < 0) {
  132.         count[i] += biggest; /* += 2*biggest for mid step version */
  133.         stepper(i);
  134.         }
  135.     }
  136.     }
  137.  
  138.     for (i=0;i<n;i++) {        /* sanity check... */
  139.     assert(count[i] == 0);    /* == biggest for mid step version */
  140.     }
  141. }
  142.  
  143. /* handy for testing without motors */
  144. void print_step(int which)
  145. {
  146.     printf("stepping %d\n", which);
  147. }
  148.  
  149. /* test driver.  read step counts from the command line, then print
  150.    out the stepping pattern */
  151. main(int argc, char *argv[])
  152. {
  153.     int *dx;
  154.     int i, n;
  155.  
  156.     n = argc-1;
  157.     dx = calloc(n, sizeof(dx[0]));
  158.     assert(dx);
  159.  
  160.     for (i=0;i<n;i++) {
  161.     sscanf(argv[i+1], "%d", &dx[i]);
  162.     }
  163.  
  164.     coordinate(dx, n, print_step);
  165. }
  166.     
  167. --
  168.  
  169. REALLY, instead of hyping low impact soles, I think what we need
  170. are more low impact souls.
  171.                     Greg Jahn
  172.