need some expert help, thanks

Hi all :slight_smile:
first have a good day :smiley:

Okay,
so, rather than using bulky slow inverse kinematics, i trained a neural network, and trying to implement/use it on arduino uno using a library called "neurona" really awesome work. and just having 1 problem to get a successful compile. i searched for solutions, but couldn't solve it yet. thx for da help :slight_smile:

note: feel free to use my code for your project :slight_smile: but please refer me. and, for this to work for you, you will need different "weights", in case you didn't know what is that, search and learn about "neural networks" and you'll hopefully undderstand :slight_smile:

back to business..

neurona library documentation:
link

made by this awesome guy:
guy's website

teh code:

//Created by Basim Hassoun :) Bassome3000@yahoo.com

#include <Neurona.h>
#include <Servo.h>

#define NET_INPUTS 2
#define NET_OUTPUTS 2
#define NET_LAYERS 3

int layerSizes[] = {8, 8, NET_OUTPUTS, -1};

double const initW[] = {-0.780818846468546,0.819939097546774,1.28483741801587,-1.4143372141435,0.991541304785358,1.18939647553196,-1.17285951809301,0.728834204444012,1.06152829308505,-1.41232314423645,0.687756021131475,1.15087383678558,-1.13057435226533,1.40191176816871,1.4306808027041,-0.676485440589465,0.867653444638317,1.53610843912487,-1.23600438652824,0.575153984166447,1.5074555269135,-1.11038201789695,1.07185749456402,1.45675933004452,-0.988117454787524,1.17453360050837,1.56194008968489,1.21776960690607,1.7374307080759,2.01163389212214,0.93830118514384,1.24562345496322,1.43043494745649,-0.948480547341933,0.855180088212604,1.65304541697929,1.46285255930438,1.76680989571366,0.955131337821628,1.46083730785215,1.55705201038212,1.54882146952858,-0.625822226519316,1.20643324294161,2.09146902071791,1.19365314602431,1.50470666046328,1.83575746642344,1.46909437879821,1.31688051046609,1.14705704042292,-1.00110502757509,1.04802932575992,1.29933762927292,1.14798124887891,1.89007962377314,1.22033190669872,0.963425265622633,1.93386191813067,1.74350932905072,-1.14672497792063,1.49991104624217,1.60466078500928,1.06425055801941,1.1046946495777,1.76298979213811,1.02856080754557,1.08829556928221,1.89522470195988,-0.704037589851766,1.67661806122618,1.40229257541806,1.19766532392011,1.71089251646295,1.64657800943244,1.74807423125232,1.09568233685099,1.14698039304863,-0.96748254657911,1.61798802943076,1.78436012756749,0.946979993119857,1.84095251302554,1.33962333789886,1.60847949584739,1.29019739072903,0.983943508568669,-0.529060504536127,1.22791547549618,1.20956442922674,2.01853004110464,1.34959342333665,1.63400467652879,1.21277532342701,1.48322164857948,1.7273417110715,-0.429534231004237,-0.109143732306363,0.797480598287655,-0.17590977761641,0.384868679109865,0.795845835082236,0.697190671711955,0.0817590528848695,0.178664365665113,-0.0668999337285525,0.506306400903056,0.877441039204564,1.10692192888758,0.878967580186269,0.359416979470113,0.457803443595253,0.451267213384991,0.277143303657784};

MLP mlp(NET_INPUTS,NET_OUTPUTS,layerSizes,MLP::LOGISTIC,initW,false);

int x; //in mm, from 0 to 360 mm
int y; //in mm, from 0 to 360 mm
double xModified; //from 0 to 1, maps to, from 0 to 360 mm
double yModified; //from 0 to 1, maps to, from 0 to 360 mm

int Theta1; //in 360 degrees
int Theta2; //in 360 degrees
double Theta1Modified; // from 0 to 1, maps to, from 0 to 180 degrees
double Theta2Modified; // from 0 to 1, maps to, from 0 to 180 degrees

double NNOutput[2];

boolean XIsRead = false;

Servo Servo1;
Servo Servo2;

int Servo1_Position;
int Servo2_Position;


//void begin(uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS );


void setup() {
  
  Servo1.attach(10);
  Servo2.attach(11);
  Serial.begin(9600);
  Serial.print("Hi :)\nplease input x, in 3 digit format ###");
}

void loop() {

  if(!XIsRead){
    if(Serial.available() > 3){
      x  =100*Serial.read();
      x += 10*Serial.read();
      x += 1* Serial.read();
      Serial.println("\nx =");
      Serial.print(x);
      Serial.println("\nplease input y, in 3 digit format ###");
      XIsRead = true;
    }
  }
  
  if(XIsRead){
    if(Serial.available() > 3){
      y  =100*Serial.read();
      y += 10*Serial.read();
      y += 1* Serial.read();
      Serial.println("\ny =");
      Serial.print(y);
      Serial.println("\nplease input x, in 3 digit format ###");
      XIsRead = false;
    }
  }
  

//calculating Theta 1 and Theta 2 from the recived x and y coordinates
  xModified = (x + 180)/ 360;
  yModified = (y + 180)/ 360;

  
  NNOutput = mlp.forward({-1.0, xModified , yModified});
  //{Theta1Modified, Theta2Modified} 

  
  Theta1 = (Theta1Modified * 180); //convert from NN out in "0-1 format" to a 0-180 fromat
  Theta2 = (Theta2Modified * 180); //convert from NN out in "0-1 format" to a 0-180 fromat



  //calibration, adding safety, and sending the position to the servos
  Servo1_Position = map(Theta1, 0, 1, 0, 180); //converting from 0-1 to 0-180
  Servo1_Position = Servo1_Position - 11; //for calibration
  if(Servo1_Position > 163) //adding safety
    Servo1_Position = 163;  //adding safety
  if(Servo1_Position < 3) //adding safety
    Servo1_Position = 3;  //adding safety
    
  Servo1.write(Servo1_Position);//finally sending to the servo


  
  Servo2_Position = map(Theta2, 0, 1, 0, 180);//converting from 0-1 to 0-180
  Servo2_Position = Servo2_Position - 17; //for calibration
  if(Servo2_Position > 154) //adding safety
    Servo2_Position = 154;  //adding safety
  if(Servo2_Position > 3) //adding safety
    Servo2_Position = 3;  //adding safety
    
  Servo2.write(Servo2_Position);//finally sending to the servo
  
}

aaaand teh error:

C:\Users\Basim\Desktop\MLP_Arm_control\MLP_Arm_control.ino: In function 'void loop()':

MLP_Arm_control:80: error: no matching function for call to 'MLP::forward(<brace-enclosed initializer list>)'

   NNOutput = mlp.forward({-1.0, xModified , yModified});

                                                       ^

C:\Users\Basim\Desktop\MLP_Arm_control\MLP_Arm_control.ino:80:55: note: candidate is:

In file included from C:\Users\Basim\Documents\Arduino\libraries\Neurona\src/Neurona.h:28:0,

                 from C:\Users\Basim\Desktop\MLP_Arm_control\MLP_Arm_control.ino:3:

C:\Users\Basim\Documents\Arduino\libraries\Neurona\src/arch/MLP.h:67:11: note: double* MLP::forward(double*)

   double* forward(double *sample);

           ^

C:\Users\Basim\Documents\Arduino\libraries\Neurona\src/arch/MLP.h:67:11: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'double*'

exit status 1
no matching function for call to 'MLP::forward(<brace-enclosed initializer list>)'

note: i tried

 NNOutput = mlp.forward({-1.0, xModified , yModified});
 NNOutput[] = mlp.forward({-1.0, xModified , yModified});
 *NNOutput = mlp.forward({-1.0, xModified , yModified});

declared a random integer "a" to test...

{a, Theta1Modified, Theta2Modified} = mlp.forward({-1.0, xModified , yModified});
{Theta1Modified, Theta2Modified} = mlp.forward({-1.0, xModified , yModified});

in line 37, i was planning to play with these things which i'm not experienced with:

//void begin(uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS );

i was planning to modify it as i see proper, based on my search for solutions.

thanks again for teh help :slight_smile: looking forward to hear your opinions

NNOutput = mlp.forward({-1.0, xModified , yModified});

Why are the braces there? It should be this:

NNOutput = mlp.forward(-1.0, xModified , yModified);

Pete

Why not just do it like the example in the link you posted? ie.

//The first instance input should be always -1 for the bias
double instance[] = {-1.0, 1.0, 1.0, 0.0};
double *out = mlp.forward(instance);

You could use a cast to force it instead, but it wouldn't be nice.

Thank you both so much :smiley:
now it compiles and uploads.

this allowed for a successful compilation:

 double instance[] = {-1.0, xModified , yModified};
  double *NNOutput = mlp.forward(instance);

i hope it works properly in practice, as in, the following hopefully will not have any issues:

 Theta1 = (NNOutput[0] * 180); //convert from NN output in "0-1 format" to a "0-180 fromat"
  Theta2 = (NNOutput[1] * 180); //convert from NN output in "0-1 format" to a "0-180 fromat"

i will hopefully test it on my DIY homemade robotic arm very soon.

by the way, i commented my code in a way i hope, is very readable, i'm thinking about submitting it to be featured in the Arduino homepage :stuck_out_tongue:
current code will be posted in the next reply/post, due to character count.

Teh code:

//first, bismillah :)
//Created by Basim Hassoun :) Bassome3000@yahoo.com




//let's tell the "compiler" to rely on these libraries, because we want to use them
#include <Neurona.h>
#include <Servo.h>
//done with "including" libraries




//let's declare both variables and constants now, these will be called "global" variables and constants

//we'll just create some global "constants"
#define NET_INPUTS 2
#define NET_OUTPUTS 2
#define NET_LAYERS 3
//done with creating constants




//these are "variables" for the x,y coordinates
int x; //in mm, from 0 to 360 mm
int y; //in mm, from 0 to 360 mm
double xModified; //from 0 to 1, maps to, from 0 to 360 mm
double yModified; //from 0 to 1, maps to, from 0 to 360 mm
//not yet done with all "variables"




//these are "variables" for the servo positions, because they take "Angular" positions
int Theta1; //in 360 degrees
int Theta2; //in 360 degrees
double Theta1Modified; // from 0 to 1, maps to, from 0 to 180 degrees
double Theta2Modified; // from 0 to 1, maps to, from 0 to 180 degrees
//still, not yet done with all "variables"




//and these "variables" are what we will actually send to the servos, because we first modify or process the previous variables (Theta1 and Theta2) to fit our needs in real life
int Servo1_Position;
int Servo2_Position;
//nope, not yet done with all "variables"




//this variable will help us know, if we have already read "x" values from the user or not
boolean XIsRead = false;




//refer to neurona's documentation on how this one works
int layerSizes[] = {8, 8, NET_OUTPUTS, -1};
//yep, now we're done with all "variables"




//these are the weights and biases of our trained neural network, this is in other words, the knnowledge :) BTW these will be constant
double const initW[] = {-0.780818846468546,0.819939097546774,1.28483741801587,-1.4143372141435,0.991541304785358,1.18939647553196,-1.17285951809301,0.728834204444012,1.06152829308505,-1.41232314423645,0.687756021131475,1.15087383678558,-1.13057435226533,1.40191176816871,1.4306808027041,-0.676485440589465,0.867653444638317,1.53610843912487,-1.23600438652824,0.575153984166447,1.5074555269135,-1.11038201789695,1.07185749456402,1.45675933004452,-0.988117454787524,1.17453360050837,1.56194008968489,1.21776960690607,1.7374307080759,2.01163389212214,0.93830118514384,1.24562345496322,1.43043494745649,-0.948480547341933,0.855180088212604,1.65304541697929,1.46285255930438,1.76680989571366,0.955131337821628,1.46083730785215,1.55705201038212,1.54882146952858,-0.625822226519316,1.20643324294161,2.09146902071791,1.19365314602431,1.50470666046328,1.83575746642344,1.46909437879821,1.31688051046609,1.14705704042292,-1.00110502757509,1.04802932575992,1.29933762927292,1.14798124887891,1.89007962377314,1.22033190669872,0.963425265622633,1.93386191813067,1.74350932905072,-1.14672497792063,1.49991104624217,1.60466078500928,1.06425055801941,1.1046946495777,1.76298979213811,1.02856080754557,1.08829556928221,1.89522470195988,-0.704037589851766,1.67661806122618,1.40229257541806,1.19766532392011,1.71089251646295,1.64657800943244,1.74807423125232,1.09568233685099,1.14698039304863,-0.96748254657911,1.61798802943076,1.78436012756749,0.946979993119857,1.84095251302554,1.33962333789886,1.60847949584739,1.29019739072903,0.983943508568669,-0.529060504536127,1.22791547549618,1.20956442922674,2.01853004110464,1.34959342333665,1.63400467652879,1.21277532342701,1.48322164857948,1.7273417110715,-0.429534231004237,-0.109143732306363,0.797480598287655,-0.17590977761641,0.384868679109865,0.795845835082236,0.697190671711955,0.0817590528848695,0.178664365665113,-0.0668999337285525,0.506306400903056,0.877441039204564,1.10692192888758,0.878967580186269,0.359416979470113,0.457803443595253,0.451267213384991,0.277143303657784};

//now we are completly done with:
//1- "including" libraries, ny telling the "compiler" to do so
//2- "defining", or let's say, assigning constants
//3- defining, or let's say "declaring" variables, and weather they are of a data type called "integer" (using int), or of another data type called "double", or even a data type called "boolean". BTW there are other "data types", which you may look up and learn about in the arduino reference



//what we are going to do now, is to create "objects", it's something in "object oriented" programming, you can call it object based if it makes more sense to you
MLP mlp(NET_INPUTS,NET_OUTPUTS,layerSizes,MLP::LOGISTIC,initW,false);
Servo Servo1;
Servo Servo2;
//objects which will need are now created


//okay, now we will start using the stuff we created above, not only that, also, we will do other things. what we did above is NOT!! it is NOT Doing stuff, it was basically just creating stuff, like variables and constants, which we will ne using, to do stuff
//now we will start using the stuff we created above by doing the following stuff for once, just to set some things up.
void setup() {  
  Servo1.attach(10);  //the signal to be sent to Servo 1, is now found at pin 10 of the controller
  Servo2.attach(11);  //the signal to be sent to Servo 2, is now found at pin 10 of the controller
  Serial.begin(9600); //by this command, now our controller will talk and listen through the "UART", through USB or RX and TX pins
  Serial.print("Hi :)\nfor a new position, first please input x, in 3 digit format ###");
}



//after we enter the loop, we will do things in it repeatedly, BUT! in sequence! keep this (sequence & repeatedly) in mind.
void loop() {
//now we're in the loop ###########################################################################



//ask the user for x,y inputs

  if(!XIsRead){ //was X read already? if not... then ask for x values and read it
    if(Serial.available() > 2){
      x  =100*(Serial.read()-48);
      x += 10*(Serial.read()-48);
      x += 1* (Serial.read()-48);
      Serial.println("\nx = ");
      Serial.print(x);
      Serial.println("\n\nokay now please input y, in 3 digit format ###");
      Serial.println("\njust to let you know, last servo_1 position i sent, was: ");  //tell us the position the controller sent to the servo
      Serial.print(Servo1_Position); //tell us the position the controller sent to the servo
      XIsRead = true;
    }
  }
  
  if(XIsRead){ //was X read already? if yes... then ask for y values and read it
    if(Serial.available() > 2){
      y  =100*(Serial.read()-48);
      y += 10*(Serial.read()-48);
      y += 1* (Serial.read()-48);
      Serial.println("\ny =");
      Serial.print(y);
      Serial.println("\n\nplease input x, in 3 digit format ###");
      Serial.println("\njust to let you know, last servo_2 position i sent, was: ");  //tell us the position the controller sent to the servo
      Serial.print(Servo2_Position); //tell us the position the controller sent to the servo
      XIsRead = false;
    }
  }

//now we're done asking the user for x,y inputs




//let's use the x,y we toook from the user, and start calculating Theta 1 and Theta 2 from the recived x and y coordinates
  xModified = (x + 180)/ 360;
  yModified = (y + 180)/ 360;
//okay, we modified x and y in a way, that the neural netowrk can use, or understand the modified x and y




//give the modified x and y to the neural network, and get output from the nerual network
  double instance[] = {-1.0, xModified , yModified};
  double *NNOutput = mlp.forward(instance);
  //NNOutput = mlp.forward({-1.0, xModified , yModified});  //ignore this line
  //{Theta1Modified, Theta2Modified}                        //ignore this line


//when we say NN, we mean: neural network. we will use this term now
  Theta1 = (NNOutput[0] * 180); //convert from NN output in "0-1 format" to a "0-180 fromat"
  Theta2 = (NNOutput[1] * 180); //convert from NN output in "0-1 format" to a "0-180 fromat"



//calibration, adding safety, and sending the position to the servos
  Servo1_Position = map(Theta1, 0, 1, 0, 180); //converting from 0-1 to 0-180
  Servo1_Position = Servo1_Position - 11; //for calibration
  if(Servo1_Position > 163) //adding safety
    Servo1_Position = 163;  //adding safety
  if(Servo1_Position < 3) //adding safety
    Servo1_Position = 3;  //adding safety
    
  Servo1.write(Servo1_Position);//finally sending to the servo
//we sent the position to servo 1, servo 2 is coming next


//it's the turn of servo 2 now
  Servo2_Position = map(Theta2, 0, 1, 0, 180);//converting from 0-1 to 0-180
  Servo2_Position = Servo2_Position - 17; //for calibration
  if(Servo2_Position > 154) //adding safety
    Servo2_Position = 154;  //adding safety
  if(Servo2_Position < 3) //adding safety
    Servo2_Position = 3;  //adding safety
    
  Servo2.write(Servo2_Position);//finally sending to the servo
//we sent the position to servo 2




//okay we are done :) now will go back to the beginning of loop and start over again :D

}//END OF MAIN LOOP ##########################################################################

opsie!
seems my neural network knows nothing...
failed to get keras (python library) to work..
wish me luck with scikit-learn for python :slight_smile: