Advertisement
j0h

WhipRobot

j0h
Oct 11th, 2023
1,166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Whip mechanics robot:
  3. uses tethered mouse feedback via motor impulse, and cord length to generate pseudo-random motion continuing from an initial motion.
  4. Possible improvements:
  5. dont use delay, or dont interupt mouse feedback, or dont stop after initial start at 0,0 crossing.
  6.    Initial start up is at 0,0, x,y +/-
  7. works ok as is.
  8. */
  9.  
  10. #include "USBHost_t36.h"
  11. //Built on a teensy 4.M1
  12. //analog io
  13. const int LF = 0;
  14. const int LR = 1;
  15. const int RF = 2;
  16. const int RR = 3;
  17.  
  18. USBHost myusb;
  19. MouseController mouse1(myusb);
  20. USBHub hub1(myusb);
  21. USBHIDParser hid1(myusb);
  22. //X:lft,rgt Y;fwd,rev
  23. void fwd(int y);
  24. void rev(int y);
  25. void lft(int x);
  26. void rgt(int x);
  27. void stp();
  28. int dtime =200; //Run Time
  29. int n=100;  //multiplyer for runtime (speed)
  30. void setup(){
  31.  //while (!Serial) ; // wait for Arduino Serial Monitor:dubug use onlu
  32.   myusb.begin();    //on board USB input
  33.   pinMode(LF,OUTPUT);
  34.   pinMode(LR,OUTPUT);
  35.   pinMode(RF,OUTPUT);
  36.   pinMode(RR,OUTPUT);
  37. }
  38.  
  39. void loop(){
  40.   myusb.Task();
  41.  
  42.  
  43.   if (mouse1.available()) {
  44.     int x = n * mouse1.getMouseX(); //X==right
  45.     int y = n * mouse1.getMouseY(); //Y==left
  46.     //gt 0=fwd
  47.     //lt 0=rverse
  48.    
  49.     if(x==0 && y==0){
  50.       stp();
  51.         //all pins ==0== stoped      
  52.       }
  53.       if(y>0){ //fwd
  54.         fwd(y);
  55.        }
  56.       if(y<0){
  57.         rev(y);
  58.         }
  59.       if(x>0){ //right?
  60.         rgt(x);
  61.         }    
  62.       if(x<0){
  63.         lft(x);
  64.         }
  65.    
  66.    /*
  67.     //Serial.print("Mouse: buttons = ");
  68.     //Serial.print(mouse1.getButtons());
  69.     Serial.print(",  mouseX = ");
  70.     Serial.print(mouse1.getMouseX());
  71.     Serial.print(",  mouseY = ");
  72.     Serial.print(mouse1.getMouseY());
  73.     //Serial.print(",  wheel = ");
  74.     //Serial.print(mouse1.getWheel());
  75.     //Serial.print(",  wheelH = ");
  76.     //Serial.print(mouse1.getWheelH());
  77.     Serial.println();
  78.  */
  79.     mouse1.mouseDataClear();
  80.   }
  81.  
  82. }
  83. //X:lft,rgt Y;fwd,rev
  84. void fwd(int y){ //pos Y
  85. Serial.println("Forward");    
  86.        analogWrite(LF,y);
  87.        analogWrite(LR,0);
  88.        analogWrite(RF,y);
  89.        analogWrite(RR,0);
  90.        delay(dtime);
  91.        stp();  
  92.   }
  93. void rev(int y){  //neg Y
  94. Serial.println("Reverse");
  95.        analogWrite(LF,0);
  96.        analogWrite(LR,y);
  97.        analogWrite(RF,0);
  98.        analogWrite(RR,y);
  99.        delay(dtime);
  100.        stp();
  101.        }
  102. void lft(int x){ //neg X
  103. Serial.println("Left");
  104.        analogWrite(LF,0);
  105.        analogWrite(LR,x);
  106.        analogWrite(RF,x);
  107.        analogWrite(RR,0);
  108.        delay(dtime);
  109.        stp();
  110.   }
  111. void rgt(int x){ //pos X
  112. Serial.println("Right");
  113.        analogWrite(LF,x);
  114.        analogWrite(LR,0);
  115.        analogWrite(RF,0);
  116.        analogWrite(RR,x);
  117.        delay(dtime);
  118.        stp();
  119.        }
  120. void stp(){
  121. Serial.println("Stop");
  122.        analogWrite(LF,0);
  123.        analogWrite(LR,0);
  124.        analogWrite(RF,0);
  125.        analogWrite(RR,0);
  126.        }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement