Things used in this project

Hardware components:
11113 01
SparkFun Arduino Pro Mini 328 - 5V/16MHz
×1
Motor with encoder (generic)
×1
Texas instruments l293dne image
Texas Instruments Dual H-Bridge motor drivers L293D
×1
13040 01
SparkFun Block for Intel® Edison - UART
×1
Software apps and online services:
Vs2015logo
Microsoft Visual Studio 2015

Code

Code snippet #1Arduino
void loop() {
   if (stringComplete) {
     // clear the string when COM receiving is completed
     mySt = "";  //note: in code below, mySt will not become blank, mySt is blank until '\n' is received
     stringComplete = false;
   }
  //receive command from Visual Studio
   if (mySt.substring(0,8) == "vs_start"){
     digitalWrite(pin_fwd,1);      //run motor run forward
     digitalWrite(pin_bwd,0);
     motor_start = true;
   }
   if (mySt.substring(0,7) == "vs_stop"){
     digitalWrite(pin_fwd,0);
     digitalWrite(pin_bwd,0);      //stop motor
     motor_start = false;
   }
   if (mySt.substring(0,12) == "vs_set_speed"){
     set_speed = mySt.substring(12,mySt.length()).toFloat();  //get string after set_speed
   }
   if (mySt.substring(0,5) == "vs_kp"){
     kp = mySt.substring(5,mySt.length()).toFloat(); //get string after vs_kp
   }
   if (mySt.substring(0,5) == "vs_ki"){
     ki = mySt.substring(5,mySt.length()).toFloat(); //get string after vs_ki
   }
   if (mySt.substring(0,5) == "vs_kd"){
     kd = mySt.substring(5,mySt.length()).toFloat(); //get string after vs_kd
   } 
 }
void detect_a() {
   encoder+=1; //increasing encoder at new pulse
   m_direction = digitalRead(pin_b); //read direction of motor
 }
 ISR(TIMER1_OVF_vect)        // interrupt service routine - tick every 0.1sec
 {
   TCNT1 = timer1_counter;   // set timer
   pv_speed = 60.0*(encoder/200.0)/0.1;  //calculate motor speed, unit is rpm
   encoder=0;
   //print out speed
   if (Serial.available() <= 0) {
     Serial.print("speed");
     Serial.println(pv_speed);         //Print speed (rpm) value to Visual Studio
     }
  //PID program
   if (motor_start){
     e_speed = set_speed - pv_speed;
     pwm_pulse = e_speed*kp + e_speed_sum*ki + (e_speed - e_speed_pre)*kd;
     e_speed_pre = e_speed;  //save last (previous) error
     e_speed_sum += e_speed; //sum of error
     if (e_speed_sum >4000) e_speed_sum = 4000;
     if (e_speed_sum <-4000) e_speed_sum = -4000;
   }
   else{
     e_speed = 0;
     e_speed_pre = 0;
     e_speed_sum = 0;
     pwm_pulse = 0;
   }
  //update new speed
   if (pwm_pulse <255 & pwm_pulse >0){
     analogWrite(pin_pwm,pwm_pulse);  //set motor speed 
   }
   else{
     if (pwm_pulse>255){
       analogWrite(pin_pwm,255);
     }
     else{
       analogWrite(pin_pwm,0);
     }
   }
}
Code snippet #2Arduino
#pragma endregion
 private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
 serialPort1->Open();
 timer1->Start();
 mStr = "0";
 i=300;
 }
 private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
 serialPort1->WriteLine("vs_set_speed"+textBox1->Text); //send set_speed to Arduino
 serialPort1->WriteLine("vs_kp"+textBox2->Text); //send kP to Arduino
 serialPort1->WriteLine("vs_ki"+textBox3->Text); //send kI to Arduino
 serialPort1->WriteLine("vs_kd"+textBox4->Text); //send kD to Arduino
 }
 private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) {
 String^ length;
 length=mStr->Length.ToString();
 if(mStr->Substring(0,5)=="speed"){
 speed=mStr->Substring(5,System::Convert::ToInt32(length)-6);
 label1->Text=speed;
 //print motor speed into Chart
 this->chart1->Series["Series1"]->Points->AddXY(i,System::Convert::ToDouble(speed));
 i++;
 this->chart1->ChartAreas["ChartArea1"]->AxisX->Minimum=i-300; //shift x-axis
 }
 }
 private: System::Void serialPort1_DataReceived(System::Object^  sender, System::IO::Ports::SerialDataReceivedEventArgs^  e) {
 mStr=serialPort1->ReadLine();
 }
 private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
 serialPort1->WriteLine("vs_start"); //start motor
 }
 private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {
 serialPort1->WriteLine("vs_stop");  //stop motor
 }

Credits

Replications

Did you replicate this project? Share it!

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

End-Effector and Control Logic for Robot
Intermediate
  • 3,202
  • 24

For my thesis, I have developed the control system and end-effector for a robot, easily implementable and economic.

Theremino Adapter for CNC.
Intermediate
  • 293
  • 7

Full instructions

Attention: This adapter is used to replace the parallel port with the USB, and not to operate Mach3 CNC or Linux.

Theremino Logger
Intermediate
  • 164
  • 4

Datalogger is simple to use but with great performance. The base, concise and intuitive.

Theremino - Access to Slots “WriteSlot”  “ReadSlot”.
Intermediate
  • 46
  • 3

Full instructions

A description of how to communicate with the slots of Theremino System by simply calling the functions: “WriteSlot” and “ReadSlot”.

Theremino - Use the serial port to write data to Slots.
Intermediate
  • 80
  • 3

Theremino_SeriHAL lets you send data to slots from a serial port.

Pulse Train HAT Pick & Place Example
Intermediate
  • 424
  • 3

Protip

With this example, we show how to use the PTHAT and Raspberry Pi being used to learn commands for pick and place applications.

Add projectSign up / Login
Respect project