Controll a stepper motor

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 5.
To post in this forum you must have a registered copy of Flowcode 5 or higher.

Moderator: Benj

Post Reply
User avatar
Jordy101091
Posts: 519
Joined: Sat Jan 08, 2011 4:02 pm
Location: The Netherlands
Has thanked: 25 times
Been thanked: 188 times
Contact:

Controll a stepper motor

Post by Jordy101091 »

Hi, all

Im working on a new application its called a ADS-B Airplane Tracker.
The goal of this application is to point a camera in the direction of an airplane by GPS- coordinates.

Therefore I want to use 2 stepper motors:

- one for the rotating movement
- second for the up- down motion of the camera

The first problem that I'm having is to control the movement of the stepper motor. What I'm trying to do is as follow:

When the system is powered for the very first time the system needs to by calibrated, the user needs to manually move the stepper motor to its home position.
When this step is completed a small test program runs, that moves the motor at its maximum speed in 45 degree steps. After 1 step of 45 degrees the program delays for a curtain time and makes the next step of 45 degree, until the the stepper motor returns to its home position.

Then the program enters its main cycle, here the program waits for a useable GPS fix and determines its location after valid data is received the user can now enter the GPS information that needs to be pointed at. I don't know how to calculated this, maybe somebody out there have some idea on how to do this.

but the main goal for now is the have the movement of the stepper working.
I have tried the following approach:

Move stepper to home position ->
Set "stepper_position" to 0

for a 45 degree step the motor need to make 50 0.9 degree steps assuming the driver is at half/step mode for full/step mode this would be 25 1.8 degree steps.
So every time I send a clock plus to the driver chip "l297" I will count 1 up by variable "stepper_position", then if "stepper_position" is equal to 50 or 25 depends on the step mode I know that my motor has turned 45 degrees. here I will wait some time after that I will repeat the above step until "stepper_position" equals 100 or 50 then I know my motor is at its 180 degree mark. and so on.

I don't know if this will work or if its accured I will try this tomorrow, I will let you know what the results are. if anybody has any comment on this please let me know.

thanks, Jordy Kleian
the will to learn, should not be stopped by any price

User avatar
Jordy101091
Posts: 519
Joined: Sat Jan 08, 2011 4:02 pm
Location: The Netherlands
Has thanked: 25 times
Been thanked: 188 times
Contact:

Re: Controll a stepper motor

Post by Jordy101091 »

Hi, all

I have made a simple program that turns the stepper motor every 45 degrees or 25 steps or 50 steps depending on how you set the stepping mode.
In my case I will use full-stepping mode thus, 25 steps result in 45 degree turn.

In my program I have used a switch icon with 4 different outputs. In every output the stepper motor turns 25 steps the same is for output 2 and so on.
But it doesn't work as asspected. it looks like the motor does indeed make 45 degrees every time but every 1 resolution the postion of the 45 degree (mark) shifts a little bit.

But when I modifie the program to make two 180 degree turns it works correctly.

I will upload the program as soon as I get home, Im ad school now so I cant do it from here.
Hope somebody can help me.

Regards Jordy
the will to learn, should not be stopped by any price

User avatar
Jordy101091
Posts: 519
Joined: Sat Jan 08, 2011 4:02 pm
Location: The Netherlands
Has thanked: 25 times
Been thanked: 188 times
Contact:

Re: Controll a stepper motor

Post by Jordy101091 »

Hi Everyone,

My stepper motors are working as I inteded.
Now for my next challange I need to do some heavy mathematical calculations, At least I think.

What I need to calculate are the following things:

The angle between different co-ordinates. Umm let me say different:

I need to calculated the angle that my first stepper motor needs to rotate to. So that the platform is pointing towards the aircraft in a straight line. This angle need to be calculated from the systems longtitude and latitude and from the aircraft I want to point at. Wen I know that, I need to calculate the the tilting angle for my second stepper motor so that the camera is poiting directly to that aircraft.

The calculations are done on a windows machine that send the information to the controller.

I hope somebody can help me figure this out with me.

Regards Jordy
the will to learn, should not be stopped by any price

User avatar
JohnCrow
Valued Contributor
Valued Contributor
Posts: 1367
Joined: Wed Sep 19, 2007 1:21 pm
Location: Lincolnshire
Has thanked: 364 times
Been thanked: 716 times
Contact:

Re: Controll a stepper motor

Post by JohnCrow »

Hi Jordy

Have you seen this article Benj posted some time ago. It could be along simliar lines to your project though it uses servos rather than stepper motors.

http://www.matrixmultimedia.com/article.php?a=361
1 in 10 people understand binary, the other one doesn't !

User avatar
Jordy101091
Posts: 519
Joined: Sat Jan 08, 2011 4:02 pm
Location: The Netherlands
Has thanked: 25 times
Been thanked: 188 times
Contact:

Re: Controll a stepper motor

Post by Jordy101091 »

Hi all,

I finally figure it out on how to calculate the different angles. But when one problems is solved it creates a nother.
I want to send the data that I have calculated and modified via the RS232 port, that works very nice but I dont know how to figure out
How I can tell to the microcontroller witch data is what.

For example I send the following information:

8 => Tilting_Angle
110 => Rotation_Angle

How does the microcontroller knows where the 8 is for the same is for 110. The Micro doesnt know what to do whit it.
How do I solve this problem.

Regards Jordy
the will to learn, should not be stopped by any price

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Controll a stepper motor

Post by Benj »

Hello,

One options is to always send all the data in a packet.

eg.

startbyte, tilt angle, rotation angle.

This way you wait for the first byte to be received. Once you have it you try and collect another two bytes. If the other two bytes were received correctly then assign to the control variables.

Another way is to do command, data type packets.

eg

1, tilt angle
or
2, rotation angle.

Here you always try and receive two bytes. If both bytes are received correctly then you put the data into the correct control variable specified by the first byte.

A bit of work with the timeouts for your secondary bytes should give you a very reliable working system.

User avatar
Jordy101091
Posts: 519
Joined: Sat Jan 08, 2011 4:02 pm
Location: The Netherlands
Has thanked: 25 times
Been thanked: 188 times
Contact:

Re: Controll a stepper motor

Post by Jordy101091 »

He Benj Thanks for your explanation, So basically for every part of information you would send a Identifier byte with it, Im going to try this ASAP and let you know what the progress is.

But how do I receive a byte ore multiple bytes, the only option the RS232 macro gives me is to receive Char(s) or String(s), can you explain this to on how to accomplish this.

The Next thing I don't get to works is the following thing, I have build A complete interface for my project with Visual Basic 2010. Writing A string to the serial port and receiving this with my uC works perfectly. But when I'm trying to send information (in String Format) to the Serial port I cannot read it at all.

I have tried the following command in visual basic,

Code: Select all

SerialPort1.ReadLine()
and put that information into a String variable called "Sys_Temp_Reciefed", I get no result Its just like the program hangs and waits for something to happen. I have also tried to send the vbClFl command with the information, but that gives the same result.

Code: Select all

SerialPort1.ReadExisting()
and put the information into a String Variable " " . The same result, no information is inserted into the String Variable.

Code: Select all

SerialPort1.ReadByte()
Gives my some result but not what I expected, I receive number like 67, 68, 69 I don't know whats going one because I'm sending a string containing the integer number (331, 332, ...) from a DS18B20 sensor.

Benj, do you want to help fix this problem, I have never worked intensively with visual basic, I do know the basic stuff, thats about it. I really want to learn how to correctly use the serial port function.

I'm waiting for you're responds,

Regards Jordy

PS. If it helps, I can send you the project file of my VB project with Private Mail, Also I won't be able to respond before +/- 17.00
the will to learn, should not be stopped by any price

User avatar
Jordy101091
Posts: 519
Joined: Sat Jan 08, 2011 4:02 pm
Location: The Netherlands
Has thanked: 25 times
Been thanked: 188 times
Contact:

Re: Controll a stepper motor

Post by Jordy101091 »

Hi Again,

I'm hoping somebody has the time to help me figure out this problem i'm having.
The problem is as follow:

--- I have made a visual basic program that calculates different angles based on two sets of gps coordinates. After I clicked the button "Request To Send" the visual basic program send executes the following commands:

Code: Select all

SerialPort1.Write("M1")
SerialPort1.Write(Convert.ToString(StepperNumber_TiltingAngle))
SerialPort1.Write("M2")
SerialPort1.Write(Convert.ToString(StepperNumber_BearingAngle))
The meaning of M1 and M2 is for the micro controller to somehow Identify the information that I'm sending.

--- In the Flowcode program Im using a Receive Interrupt "RXINT" in the attached macro I'm doing to following steps:
Im using a macro to receive a string I suspect that I receive "M1" first so after that macro a compare the received string to "M1" if the result is true (booth string are the same) then continue to receive another string, the next string contains the "StepperNumber_tiltingAngle" after converting this back to a integer I display this value on a LCD Screen.

If this is done I repeat this sequence for "M2" and " StepperNumber_BearingAngle".

The structure of this macro is as follow:

ReceiveString
Compare_Strings
--If Strings are the same Yes >>> Receive StepperNumber_TiltingAngle >>> Convert to Integer and Display on LCD

No

ReceiveString
CompareString
--If Strings are the same Yes >>> Receive StepperNumber_BearingAngle >>> Convert to Integer and Display on LCD

No

Exit Return to Main


The problem is that I receive StepperNumber_TiltingAngle correctly but not all the time, sometimes I need to click more then ones on "Request To Send" Before the program receives the correct information.
The Program Never receives or displays the StepperNumber_BearingAngle.

I don't know what I have missed,
Hope somebody base any suggestions

Regards Jordy,

PS. I will upload my flowcode program tonight, If anybody needs the Visual Basic program give me a reason and I will send it to you by private Mail.
Attachments
Flowcode1.fcf
(14.66 KiB) Downloaded 271 times
the will to learn, should not be stopped by any price

User avatar
Jordy101091
Posts: 519
Joined: Sat Jan 08, 2011 4:02 pm
Location: The Netherlands
Has thanked: 25 times
Been thanked: 188 times
Contact:

Re: Controll a stepper motor

Post by Jordy101091 »

I just wanted to give you guys a quick reply,

I have figured out that in the Receive_RS232 Macro, That only the top part of the code is exequted properly.
What I mean By that is:
ReceiveString
Compare_Strings
--If Strings are the same Yes >>> Receive StepperNumber_TiltingAngle >>> Convert to Integer and Display on LCD

No
That only the information is received from the first part of the code (stated above) The second part appears to be unchanged. If I turns these to around so that the second part is on top (and change this in visual basic) then the correct value is displayed for StepperNumber_BearingAngle and NOT for StepperNumber_TitlingAngle, this value is 0 no matter wath I do.

Also the String Compare function doesn't work as aspected.

When I do the following in a string manipulation bock: (Example)

Code: Select all

String = Compare$(String_from_VB, "M1",1)
To make the statement of the decision box to go to Yes I need to do the following thing:

If String <> 255 This only works when I send "M11" witch is naturally wrong because "M1"is not equal to "M11", But when I try this If String = 0 It wont do anything at all but this should be correct because "M1"is equal to "M1"

I dont understand,

Hope somebody can help me.

Thanks in advance, Jordy
the will to learn, should not be stopped by any price

User avatar
Jordy101091
Posts: 519
Joined: Sat Jan 08, 2011 4:02 pm
Location: The Netherlands
Has thanked: 25 times
Been thanked: 188 times
Contact:

Re: Controll a stepper motor

Post by Jordy101091 »

Hi all, Again :shock:

I have some news good and slightly Less good :lol:

I have fixed the error when recieving two strings, The problem seems to be in the way of writing the strings with VisualBasic and Flowcode's RS232 Macro NumBytes() when receiving strings.
I hope I have said this wright forgive me if not :idea:

So for example if you send A string with visualbasic like this:

Code: Select all

SerialPort1.Write("52")
This will send a string containing the number 52 witch is 2 bytes 5 and 2.
In flowcode you need to define parameters within the RS232 Macro these are:

Code: Select all

nTimeOut(Byte) and NumBytes(Byte)
What I have done here is set these parameters as follow nTimeOut(Byte) = 1 and NumBytes(Byte) = 4
What this means is that the macro itself waits for a period of time and it NEEDS to receive 4 bytes.
Well I'm sending 2 bytes with visualbasic and my uC program needs 4 bytes so I have 2 bytes short, as a result that the information gets corrupted.

I was thinking for a wile (2 days) what the best possible solution was for fixing this problem,bearing in mind that I designed this whole application so that the PC needs to do almost all the hard work.
I have writing a peace of code that checks how many chars the string contains that I want to write to the serialport.

Code: Select all

'Here we determine how many characters the variable "Send_Info_Motor1" Contains
'We also need that the system must have 4 bytes to receive otherwise the data gets corrupted


        Send_Info_Motor1 = StepNumber_TiltingAngle
        Send_Info_Motor2 = StepNumber_Degrees_Bearing

        Motor1_info_Container = Len(Send_Info_Motor1)
        Motor2_Info_Container = Len(Send_Info_Motor2)

        Str_Char_Short1 = 4 - Motor1_info_Container
        Str_Char_Short2 = 4 - Motor2_Info_Container

        If Str_Char_Short1 = 1 Then
            Send_Info_Motor1 = Send_Info_Motor1 + " "
        ElseIf Str_Char_Short1 = 2 Then
            Send_Info_Motor1 = Send_Info_Motor1 + "  "
        ElseIf Str_Char_Short1 = 3 Then
            Send_Info_Motor1 = Send_Info_Motor1 + "   "
        End If

        SerialPort1.Write(Send_Info_Motor1)

        If Str_Char_Short2 = 1 Then
            Send_Info_Motor2 = Send_Info_Motor2 + " "
        ElseIf Str_Char_Short2 = 2 Then
            Send_Info_Motor2 = Send_Info_Motor2 + "  "
        ElseIf Str_Char_Short2 = 3 Then
            Send_Info_Motor2 = Send_Info_Motor2 + "   "
        End If
        
        SerialPort1.Write(Send_Info_Motor2)
What the following code does is adding the missing chars or bytes (just the way you look at it) to the string that I want to send.
So for Example 52

This result is 2 missing bytes so we need to add those:

"52" + "**" (for the stars you can put Zero's, spaces etc.)

The Flowcode then converts the string to integers and will not be boddert with the ** or 00 or xx
The result is 52 correctly displayed.

For the problem that I'm having now is determine what the differens is between the new and old variable is that I'm receiving for example,

The first number of steps im recieving is lets say 50
After A while the second number im receiving is 42

The diference between these two is 42 - 50 = -8 So here I need to go 8 steps back

If the first number I receive is 42
The second is 50
The result of the two = 50 - 42 = 8 So here I need to advance 8 steps

Can anybody help me on how to program this in flowcode

Thanks in advance, Jordy
the will to learn, should not be stopped by any price

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Controll a stepper motor

Post by Benj »

Hello,

I think I would probably do something like this.

if (old_num < new_num)
{
direction = 1
stepcount = new_num - old_num
}

if (old_num > new_num)
{
direction = 0
stepcount = old_num - new_num
}

Then process the direction and step count variables to get to the right position.

I would also maybe think about creating a variable packet structure if this is causing problems. Eg the first byte you send from VB could tell the micro the number of bytes to follow.

User avatar
Jordy101091
Posts: 519
Joined: Sat Jan 08, 2011 4:02 pm
Location: The Netherlands
Has thanked: 25 times
Been thanked: 188 times
Contact:

Re: Controll a stepper motor

Post by Jordy101091 »

Hi benj,

Thank you very much for your reply it works awesome.
I will post a video of the results so far.

Another one,

I have bought a gps module from adafruit.com https://www.adafruit.com/products/660
I hope this module works with the GPS macro from flowcode 5, I think it does because the gps block uses the UP500.

Can you tell me this?

Is it possible to use multiple serial devices on one uart port.
As you probably know I'm using a RS232 USB Eblock at the moment but would like to use a GPS module as well.

and what is the best way to do this in software.

Thanks in advance,

Jordy Kleian
the will to learn, should not be stopped by any price

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Controll a stepper motor

Post by Benj »

Hello,

Yes that GPS module should work fine. Make sure you change the GPS component properties so that it is using the 9600 baud setting and you should be good to go.

Multiple devices on one UART is not recommended in asynchronous mode.

The GPS component relies on a receive interrupt so this really needs to be a hardware UART at the moment. The interrupt is used because there is a lot of data that comes through at once and using software means that you have to be constantly ready in your program to receive the data. Using hardware and the interrupt means your program does not have to take anything into account and should simply work every time.

A software UART could be used for the USB232 board. With this your main problem will be with the receive function (data from PC to micro) as there is no longer a hardware peripheral constantly monitoring the incoming signal.

Should you need to improve this I would ensure the receive pin is connected to one of the devices INT pins. I would then enable an interrupt in Flowcode to trigger on the falling edge of that INT pin. Inside the interrupt macro perform a RS232 read byte macro and this should be reliable at collecting the incoming data.

User avatar
Jordy101091
Posts: 519
Joined: Sat Jan 08, 2011 4:02 pm
Location: The Netherlands
Has thanked: 25 times
Been thanked: 188 times
Contact:

Re: Controll a stepper motor

Post by Jordy101091 »

Ok thanks again for your reply,

I will check it out as soon as I get the GPS Module, Will let you know.

Thanks Again
the will to learn, should not be stopped by any price

Post Reply