Recent

Author Topic: lazarus Arduino servo control  (Read 2737 times)

flori

  • Full Member
  • ***
  • Posts: 196
lazarus Arduino servo control
« on: January 06, 2020, 05:22:57 pm »
Hi everyone!
Does anyone know how to assign a button  for example: (stop servo, start, rotate 180 ) in lazarus to control the servo motor.  So! I want to control button with the servo motor.
I use SdpoSerial1 and COM7 port is active.
Thank you.
This is my arduino code:

#include <Servo.h>
Servo myservo;
int pos=0;
void setup() {
  // put your setup code here, to run once:
myservo.attach(5);
Serial.begin(9800);

}

void loop() {
  // put your main code here, to run repeatedly:
for (pos=0; pos<180; pos++){
  myservo.write(pos);
  delay(100);
  Serial.print("Servo pos: ");
  Serial.println(pos);
}
}

MarkMLl

  • Hero Member
  • *****
  • Posts: 6682
Re: lazarus Arduino servo control
« Reply #1 on: January 06, 2020, 05:54:54 pm »
I don't see the connection between this and Lazarus. Please explain what you're asking us to do.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

flori

  • Full Member
  • ***
  • Posts: 196
Re: lazarus Arduino servo control
« Reply #2 on: January 06, 2020, 06:09:01 pm »
This>

flori

  • Full Member
  • ***
  • Posts: 196
Re: lazarus Arduino servo control
« Reply #3 on: January 06, 2020, 06:09:59 pm »
I would like to control  servo motor with lazarus

MarkMLl

  • Hero Member
  • *****
  • Posts: 6682
Re: lazarus Arduino servo control
« Reply #4 on: January 06, 2020, 06:12:34 pm »
And...? That's running on a PC isn't it? The code you've shown us is running on an Arduino. Where's the connection?

/Specifically/, what in the Arduino code is waiting for commands- in some format or other- to arrive over the serial port from a PC? Once you've got that you can look at generating the commands on the PC, but not before.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

flori

  • Full Member
  • ***
  • Posts: 196
Re: lazarus Arduino servo control
« Reply #5 on: January 06, 2020, 06:18:01 pm »
how do you cite it? Which button to do?

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, SdpoSerial, Forms, Controls, Graphics, Dialogs,
  StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Label1: TLabel;
    ListBox1: TListBox;
    SdpoSerial1: TSdpoSerial;
    procedure FormCreate(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  SdpoSerial1.Active:=true;
end;

MarkMLl

  • Hero Member
  • *****
  • Posts: 6682
Re: lazarus Arduino servo control
« Reply #6 on: January 06, 2020, 06:20:06 pm »
You have code running on an Arduino. You have a project on a PC. You have no connection between them that's YOUR problem.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

flori

  • Full Member
  • ***
  • Posts: 196
Re: lazarus Arduino servo control
« Reply #7 on: January 06, 2020, 06:22:08 pm »
YEEEEEEEES!!!!!! BUT!!!! how do i connect?  >:(


Thaddy

  • Hero Member
  • *****
  • Posts: 14359
  • Sensorship about opinions does not belong here.
Re: lazarus Arduino servo control
« Reply #8 on: January 06, 2020, 06:26:57 pm »
rs232 over usb?
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

flori

  • Full Member
  • ***
  • Posts: 196
Re: lazarus Arduino servo control
« Reply #9 on: January 06, 2020, 06:29:19 pm »
 I need pascal code how do I connect between arduino and lazarus!

MarkMLl

  • Hero Member
  • *****
  • Posts: 6682
Re: lazarus Arduino servo control
« Reply #10 on: January 06, 2020, 06:31:30 pm »
rs232 over usb?
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

BasicOne

  • New Member
  • *
  • Posts: 15
Re: lazarus Arduino servo control
« Reply #11 on: January 06, 2020, 07:49:53 pm »
First of all, you have to define a communication protocol between your Arduino and the PC. This may be binary or text based. I like text based because this may be debbugged and tested easier, e.g. with a standard terminal application on the PC.

In Arduino, you have to implement a command parser accordingly, e.g. like

Code: C  [Select][+][-]
  1.  
  2. String currentCommand="";
  3. String currentResult="";
  4. String currentData="";
  5. unsigned long lastActivity=0;
  6.  
  7.  
  8. void loop() {
  9.  
  10.  
  11.   boolean crlf=true;
  12.  
  13.   if (Serial.available())
  14.   {
  15.     lastActivity=millis();
  16.     crlf=false;
  17.  
  18.     while (!crlf)
  19.     {
  20.       if (Serial.available())
  21.       {
  22.         lastActivity=millis();
  23.         char c = Serial.read();
  24.  
  25.         if (c == '\n')
  26.         {
  27.           crlf=true;
  28.           doCommand();        
  29.           Serial.println(currentResult);
  30.         }
  31.         else
  32.         {
  33.           currentCommand+=c;
  34.         }
  35.       }
  36.       else
  37.       {
  38.         if (millis()-lastActivity>10000) {
  39.           crlf=true;
  40.           Serial.println("Command Timeout");
  41.           currentCommand="";
  42.         }
  43.       }
  44.     }
  45.   }
  46. }
  47.  
  48.  
  49.  
  50. void doCommand()
  51. {
  52.   currentCommand.toUpperCase();
  53.   currentCommand.trim();
  54.   currentCommand=currentCommand+"     ";
  55.   int spacePosition1=currentCommand.indexOf(" ");
  56.  
  57.   String baseCommand=currentCommand.substring(0,spacePosition1);
  58.   baseCommand.trim();
  59.  
  60.   int spacePosition2=currentCommand.indexOf(" ",spacePosition1+1);
  61.   String parameter1=currentCommand.substring(spacePosition1,spacePosition2);
  62.   parameter1.trim();
  63.  
  64.   spacePosition1=currentCommand.indexOf(" ",spacePosition2+1);
  65.   String parameter2=currentCommand.substring(spacePosition2,spacePosition1);
  66.   parameter2.trim();
  67.  
  68.   spacePosition2=currentCommand.indexOf(" ",spacePosition1+1);
  69.   String parameter3=currentCommand.substring(spacePosition1,spacePosition2);
  70.   parameter3.trim();
  71.  
  72.   spacePosition1=currentCommand.indexOf(" ",spacePosition2+1);
  73.   String parameter4=currentCommand.substring(spacePosition2,spacePosition1);
  74.   parameter4.trim();
  75.  
  76.  
  77.   currentResult="Unknown command: "+currentCommand;
  78.  
  79.  
  80.   if (baseCommand=="IDN?")
  81.   {
  82.     currentResult="my Arduino Version";
  83.   }  
  84.  
  85.  
  86.   if (baseCommand=="POS:SET")
  87.   {
  88.     if (parameter1!="")
  89.     {
  90.       SetPosition(parameter1);
  91.       currentResult="OK";
  92.     }
  93.   }
  94.  
  95.   if (baseCommand=="POS:GET?")
  96.   {
  97.      currentResult=GetPosition();
  98.   }
  99.  
  100.   currentCommand="";
  101. }
  102.  

This is a very simple SCPI-like parser (SCPI="Standard Commands for Programmable Instruments" introduced many years ago to control measurement equipment via the IEEE488 standard).

You can connect to Arduino via COM  (a virtual COM when using USB) using a terminal application, e.g. of the Arduino IDE, and test the commands IDN?, POS:SET and POS:GET?. A command is terminated by a NewLine character and parameters are separated by space.

The sample code is not very nice but it may be easily expanded by adding other commands. The functions SetPosition() and GetPosition() have to be implemented by you, of course.

If your Arduino code is running well, you just have to replace the teminal application by your Lazarus programm and send the strings via the Lazarus serial communication component.

As mentioned above, you may also use a binary protocol that my be implemented with less code and also less traffic over the interface, but that is a bit harder to debug. In any case, it is necessary to call serial.read() somewhere in the Arduino loop to receive any data that was sent from the PC and act according the content of the received data.

I do not know the SdpoSerial Lazarus component, but I think there will be some methods like WriteData() and ReadData() to send and receive data as well as some properties defining the COM parameters as e.g. Baudrate. Just use the same COM parameters as with the terminal application used for testing before.

« Last Edit: January 06, 2020, 07:52:16 pm by BasicOne »

PaulRowntree

  • Full Member
  • ***
  • Posts: 132
    • Paul Rowntree
Re: lazarus Arduino servo control
« Reply #12 on: January 06, 2020, 09:32:06 pm »
I have had great success with LazSerial running on Win10, talking to various devices over the native COM ports, the Serial/USB adapters, and the embedded adapters.  Commercial instruments too.  I generally use ASCII communications.
Paul Rowntree
- coding for instrument control, data acquisition & analysis, CNC systems

ccrause

  • Hero Member
  • *****
  • Posts: 856
Re: lazarus Arduino servo control
« Reply #13 on: January 07, 2020, 05:59:00 am »
I'll adapt your code snippets to a trivial implementation, hopefully it is easy enough to understand the principles.  Note that this is untested code, so expect some errors, this is just to demonstrate a possible way of implementing your requirements.
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, SdpoSerial, Forms, Controls, Graphics, Dialogs,
  9.   StdCtrls;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Label1: TLabel;
  17.     ListBox1: TListBox;
  18.     SdpoSerial1: TSdpoSerial;
  19.     procedure ButtonConnect: TButton;
  20.     procedure ButtonConnectClick(Sender: TObject);
  21.     procedure ButtonLeft: TButton;
  22.     procedure ButtonLeftClick(Sender: TObject);
  23.     procedure ButtonRight: TButton;
  24.     procedure ButtonRightClick(Sender: TObject);
  25.     procedure ListBox1Click(Sender: TObject);
  26.   private
  27.     { private declarations }
  28.   public
  29.     { public declarations }
  30.   end;
  31.  
  32. var
  33.   Form1: TForm1;
  34.  
  35. implementation
  36.  
  37. {$R *.lfm}
  38.  
  39. { TForm1 }
  40.  
  41. TForm1. ButtonConnectClick(Sender: TObject);
  42. begin
  43.   SpdoSerial1.Device := 'com7'; // not sure this is the correct syntax for Windows...
  44.   SpdoSerial1.BaudRate := 9600;
  45.   SdpoSerial1.Active:=true;
  46. end;
  47.  
  48. TForm1. ButtonLeftClick(Sender: TObject);
  49. begin
  50.   SpdoSerial1.WriteData('-');
  51. end;
  52.  
  53. TForm1.ButtonRightClick(Sender: TObject);
  54. begin
  55.   SpdoSerial1.WriteData('+');
  56. end;
  57.  
  58. end.

Arduino code:
Code: C  [Select][+][-]
  1. #include <Servo.h>
  2. Servo myservo;
  3. int pos=0;
  4. void setup() {
  5.   // put your setup code here, to run once:
  6.   myservo.attach(5);
  7.   Serial.begin(9600);
  8. }
  9.  
  10. void loop() {
  11.   // put your main code here, to run repeatedly:
  12.   char cmd;
  13.   while(Serial.Available() > 0) {
  14.     cmd = Serial.Read;
  15.     if(cmd == '-'){
  16.       if(pos > 1) pos--;
  17.     }
  18.     else if(cmd == '+'){
  19.       if(pos < 180) pos++;
  20.     }
  21.   }
  22. }

A later step would be to write the Arduino code in FPC, but this is a little bit more difficult.

 

TinyPortal © 2005-2018