Advertisement
Cpt_Jellyfish

EEEE4008: Object Manipulator Auxiliary Robot Code with Omitted Switch Functionality

May 13th, 2021
535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.45 KB | None | 0 0
  1. // *** Object Manipulator Tri-Omniwheel Robot with Variable Controller Translation & Rotation Inputs
  2. // This code does not include the 'switch' button operation
  3.  
  4. //include RF  & stepper  motor libraries
  5. #include <SPI.h>
  6. #include <nRF24L01.h>
  7. #include <RF24.h>
  8. #include <AccelStepper.h>
  9. #include <Servo.h>
  10.  
  11. Servo s1;
  12. Servo s2;
  13. Servo s3;
  14. Servo s4;
  15.  
  16. //define the stepper motor pin connections
  17. const int step1Pin = 8;
  18. const int dir1Pin = 7;
  19. const int step2Pin = 6;
  20. const int dir2Pin = 5;
  21. const int step3Pin = 4;
  22. const int dir3Pin = 3;
  23.  
  24. int s1Pos = 0;
  25. int s1PosNext = 0;
  26. int s2Pos = 0;
  27. int s2PosNext = 0;
  28. int s3Pos = 0;
  29. int s3PosNext = 0;
  30. int s4Pos = 0;
  31. int s4PosNext = 0;
  32.  
  33. bool toggleDrive;
  34.  
  35. RF24 radio(9, 10);   // nRF24L01 (CE, CSN)
  36.  
  37. //configure the stepper motors
  38. AccelStepper motor1(1,step1Pin, dir1Pin); //1 is a driver interface
  39. AccelStepper motor2(1,step2Pin, dir2Pin);
  40. AccelStepper motor3(1,step3Pin, dir3Pin);
  41.  
  42. const byte address[6] = "00001"; //NRF24L01 address
  43.  
  44. //timer variables
  45. unsigned long lastReceiveTime = 0;
  46. unsigned long currentTime = 0;
  47.  
  48. //max size of this structure is 32 bytes - NRF24L01 buffer limit
  49. struct Data_Package {
  50.   byte jLValX;
  51.   byte jLValY;
  52.   byte jRValX;
  53.   byte jRValY;
  54.   byte toggleButton;
  55.   byte switchButton;
  56.   byte fireButton;
  57.   byte grabButton;
  58. };
  59.  
  60. Data_Package data; //create a variable with the above structure
  61.  
  62. //input matrix elements
  63. float a = cos((240*3.14159)/180);
  64. float b = cos((120*3.14159)/180);
  65. float c = cos(0);
  66. float d = sin((240*3.14159)/180);
  67. float e = sin((120*3.14159)/180);
  68. float f = sin(0);
  69. float g = 1;
  70. float h = 1;
  71. float i = 1;
  72.  
  73. //calculate determinant
  74. float det = (a*e*i)+(b*f*g)+(c*d*h)-(c*e*g)-(a*f*h)-(b*d*i);
  75.  
  76. //calculate inverse elements
  77. float a_inverse = ((e*i)-(f*h))/det;
  78. float b_inverse = ((h*c)-(i*b))/det;
  79. float c_inverse = ((b*f)-(c*e))/det;
  80. float d_inverse = ((g*f)-(d*i))/det;
  81. float e_inverse = ((a*i)-(g*c))/det;
  82. float f_inverse = ((d*c)-(a*f))/det;
  83. float g_inverse = ((d*h)-(g*e))/det;
  84. float h_inverse = ((g*b)-(a*h))/det;
  85. float i_inverse = ((a*e)-(d*b))/det;
  86.  
  87. //default normalised controller variables
  88. float jLValX_Normalised = 0;
  89. float jLValY_Normalised = 0;
  90. float jRValX_Normalised = 0;
  91.  
  92. //default motor speeds (set to OFF)
  93. float motor1Speed = 0;
  94. float motor2Speed = 0;
  95. float motor3Speed = 0;
  96.  
  97. void setup() {
  98.   Serial.begin(9600); //serial monitor channel
  99.  
  100.   s1.attach(5);
  101.   s2.attach(6);
  102.   s3.attach(7);
  103.   s4.attach(8);
  104.  
  105.   //setup the NRF24L01 module
  106.   pinMode(9, OUTPUT);
  107.   radio.begin();
  108.   radio.openReadingPipe(0, address);
  109.   radio.setAutoAck(false);
  110.   radio.setDataRate(RF24_250KBPS);
  111.   radio.setPALevel(RF24_PA_LOW);
  112.   radio.startListening(); //set the module as receiver
  113.   resetData(); //refresh data
  114.  
  115.   toggleDrive = true;
  116.  
  117.   //configure motor max speed and acceleration
  118.   motor1.setMaxSpeed(5000);
  119.   motor2.setMaxSpeed(5000);
  120.   motor3.setMaxSpeed(5000);
  121.   motor1.setAcceleration(1000);
  122.   motor2.setAcceleration(1000);
  123.   motor3.setAcceleration(1000);
  124.   motor1.setSpeed(0);
  125.   motor2.setSpeed(0);
  126.   motor3.setSpeed(0);
  127. }
  128.  
  129. void loop() {
  130.   readData();
  131.   delay(100);
  132.  
  133.   if (data.toggleButton == 0){
  134.     toggleDrive = !toggleDrive;
  135.   }
  136.  
  137.   if (toggleDrive == true) {
  138.     Serial.println("Drive Mode");
  139.    
  140.     readData();
  141.     delay(100);
  142.     if (data.toggleButton == 0){
  143.       toggleDrive = !toggleDrive;
  144.     }
  145.  
  146.     //insert drive code
  147.     convertJoystickVal();
  148.     //run the motors at the calculated speed
  149.     motor1.runSpeed();
  150.     motor2.runSpeed();
  151.     motor3.runSpeed();
  152.   }
  153.  
  154.   else {
  155.     //object manipulator code
  156.     Serial.println("Functionality Mode");
  157.    
  158.     s1Pos = s1.read();
  159.     s2Pos = s2.read();
  160.     s3Pos = s3.read();
  161.     s4Pos = s4.read();
  162.  
  163.     // elbow - joint 1 - base
  164.     if (data.jLValX >= 150) {
  165.       //move CC
  166.       if (s1Pos > 50){
  167.         s1PosNext = s1Pos - 1;
  168.         s1.write(s1PosNext);
  169.       }
  170.       else {
  171.       }
  172.     }
  173.  
  174.     if (data.jLValX < 100) {
  175.       //move C
  176.       if (s1Pos <= 170){
  177.         s1PosNext = s1Pos + 1;
  178.         s1.write(s1PosNext);
  179.       }
  180.       else {
  181.       }
  182.     }
  183.  
  184.     // elbow - joint 2 - middle
  185.     if (data.jLValY >= 150) {
  186.       //move CC
  187.       if (s2Pos > 90){
  188.         s2PosNext = s2Pos - 1;
  189.         s2.write(s2PosNext);
  190.       }
  191.       else {
  192.       }
  193.     }
  194.  
  195.     if (data.jLValY < 100) {
  196.       //move C
  197.       if (s2Pos <= 170){
  198.         s2PosNext = s2Pos + 1;
  199.         s2.write(s2PosNext);
  200.       }
  201.       else {
  202.       }
  203.     }
  204.  
  205.     // wrist - joint 3 - top
  206.     if (data.jRValX >= 150) {
  207.       //move CC
  208.       if (s3Pos > 10){
  209.         s3PosNext = s3Pos - 1;
  210.         s3.write(s3PosNext);
  211.       }
  212.       else {
  213.       }
  214.     }
  215.  
  216.     if (data.jRValX < 100) {
  217.       //move C
  218.       if (s3Pos <= 170){
  219.         s3PosNext = s3Pos + 1;
  220.         s3.write(s3PosNext);
  221.       }
  222.       else {
  223.       }
  224.     }
  225.  
  226.     // grabber
  227.     if (data.jRValY >= 150) {
  228.       //move CC
  229.       if (s4Pos < 105){
  230.         s4PosNext = s4Pos + 1;
  231.         s4.write(s4PosNext);
  232.       }
  233.       else {
  234.       }
  235.     }
  236.  
  237.     if (data.jRValY < 100) {
  238.       //move C
  239.       if (s4Pos > 20){
  240.         s4PosNext = s4Pos - 1;
  241.         s4.write(s4PosNext);
  242.       }
  243.       else {
  244.       }
  245.     }
  246.   }
  247.  
  248.   /*Serial.print(data.jLValX);
  249.   Serial.print(":");
  250.   Serial.print(data.jLValY);
  251.   Serial.print(":");
  252.   Serial.print(data.jRValX);
  253.   Serial.print(":");
  254.   Serial.println(data.jRValY);*/
  255. }
  256.  
  257. void resetData() {
  258.   //reset the values when there is no radio connection - set initial default values
  259.   data.jLValX = 127; //map value from 0-1023 to 0-255 (byte)
  260.   data.jLValY = 127;
  261.   data.jRValX = 127;
  262.   data.jRValY = 127;
  263.   data.toggleButton = 1; //connected to GND so 0 when pressed
  264.   data.switchButton = 1;
  265.   data.fireButton = 1;
  266.   data.grabButton = 1;
  267. }
  268.  
  269. void readData() {
  270.       // Check whether there is data to be received
  271.   if (radio.available()) {
  272.     radio.read(&data, sizeof(Data_Package)); // Read the whole data and store it into the 'data' structure
  273.     lastReceiveTime = millis(); // At this moment we have received the data
  274.   }
  275.   // Check whether we keep receving data, or we have a connection between the two modules
  276.   currentTime = millis();
  277.   if ( currentTime - lastReceiveTime > 1000 ) { // If current time is more then 1 second since we have recived the last data, that means we have lost connection
  278.     resetData(); // If connection is lost, reset the data. It prevents unwanted behavior, for example if a drone has a throttle up and we lose connection, it can keep flying unless we reset the values
  279.   }
  280. }
  281.  
  282. void convertJoystickVal(){
  283.   //check whether there is data to be received
  284.   if (radio.available()) {
  285.     radio.read(&data, sizeof(Data_Package)); //read the whole data and store it into the 'data' structure
  286.     lastReceiveTime = millis(); //at this moment the data is received
  287.   }
  288.   //check whether to keep receving data or if there is a connection between the two modules
  289.   currentTime = millis();
  290.   if ( currentTime - lastReceiveTime > 1000 ) { //if the current time is >1 second since the last data was recived then connection is lost
  291.     resetData(); //if connection is lost then reset the data - prevents unwanted behavior
  292.   }
  293.  
  294.   //left joystick x axis conversion
  295.   jLValX_Normalised = map(data.jLValX, 255, 0, 127, -127); //map(minInput, maxInput, minOutput, maxOutput)
  296.   if ((jLValX_Normalised < 5) && (jLValX_Normalised > -5)){
  297.     jLValX_Normalised = 0;
  298.   }
  299.  
  300.   //left joystick y axis conversion
  301.   jLValY_Normalised = map(data.jLValY, 255, 0, -127, 127);
  302.   if ((jLValY_Normalised < 5) && (jLValY_Normalised > -5)){
  303.     jLValY_Normalised = 0;
  304.   }
  305.  
  306.   //right joystick conversion
  307.   jRValX_Normalised = map(data.jRValX, 0, 255, 127, -127);
  308.   if ((jRValX_Normalised < 5) && (jRValX_Normalised > -5)){
  309.     jRValX_Normalised = 0;
  310.   }
  311.  
  312.   //calculate motor speeds
  313.   motor1Speed = ((a_inverse*jLValX_Normalised)+(b_inverse*jLValY_Normalised)+(c_inverse*jRValX_Normalised))*5;
  314.   motor2Speed = ((d_inverse*jLValX_Normalised)+(e_inverse*jLValY_Normalised)+(f_inverse*jRValX_Normalised))*5;
  315.   motor3Speed = ((g_inverse*jLValX_Normalised)+(h_inverse*jLValY_Normalised)+(i_inverse*jRValX_Normalised))*5;
  316.  
  317.   /*
  318.   Serial.print("M1: ");
  319.   Serial.println(motor1Speed);
  320.   Serial.print("M2: ");
  321.   Serial.println(motor2Speed);
  322.   Serial.print("M3: ");
  323.   Serial.println(motor3Speed);
  324.   */
  325.  
  326.   //run motors at the calculated speed
  327.   motor1.setSpeed(motor1Speed);
  328.   motor2.setSpeed(motor2Speed);
  329.   motor3.setSpeed(motor3Speed);
  330. }
  331.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement