Comparing input values

nyhoyn

New Member
Hi!
I'm working with a microcontroller PICAXE 20M2 for controlling a lathe tool turret and now I would need some advice on what would be a simple way of programming "comparation" of signal values between toolnumber reguest from a CNC controller and feedback values from the lathe tool turret.

The sequence is like this.

1. Microcontroller recieves toolchange request from CNC
2. Microcontroller recieves toolnumber from PLC (three outputs from the CNC I/O board to three inputs on microcontroller board)
3. Microcontroller should compare PLC output values with current position of tool turret and if different then start rotating toolturret until tool turret position value is equal with PLC output.
4. Microcontroller outputs signal "toolchange done" and CNC-controller continues with next program line.

The toolturret has a Gray type 4-bit encoder giving four inputs to the microcontroller.
enclosed table with the signall information.

Im new at programmning microcontrollers so have some patience with these questions
 

Attachments

geoff07

Senior Member
Welcome aboard!

You have a few sub-systems to deal with:

signal conditioning: so that the inputs are compatible with and safe for the Picaxe to access. If the signals are 24v they would fry the chip unless managed. Personally, I use opto-isolators for this as they are cheap and very effective, and don't introduce extra energy (as e.g. simply using a voltage divider would). The chips use micropower so keeping extraneous energy out is essential.

initiation: you will need to detect when something (anything) is happening and so begin the process. This could be an interrupt or simply scanning a signal or signals that determine action is needed

decoding: something that, once initiated, reads the inputs and gets the signals into a form that the program can use. So if three digital lines define an input value, decode them into a single number that the program can read.

Then you can use simple programming to compare the input values with the desired values, and take the appropriate action.

The key thing when doing something like this is to map it out before you start to program anything. Flowcharts are one way of doing it. Then test the design on paper. Coding is almost the last step, when you have something that is fairly complete and you have spotted the big snags before spending time on the detail.

I would also say that for clarity of programming, try to use constructs like do/loop; select/case; and if/then/else with everything defined as symbols with useful names and many subroutines for defined tasks, rather than a great kludge of code that jumps all over the place and won't make sense six months later.

I'm sure that many will help you as you get into the details, this is a good forum and you will get much support.
 

nyhoyn

New Member
Welcome aboard!

You have a few sub-systems to deal with:

signal conditioning: so that the inputs are compatible with and safe for the Picaxe to access. If the signals are 24v they would fry the chip unless managed. Personally, I use opto-isolators for this as they are cheap and very effective, and don't introduce extra energy (as e.g. simply using a voltage divider would). The chips use micropower so keeping extraneous energy out is essential.

initiation: you will need to detect when something (anything) is happening and so begin the process. This could be an interrupt or simply scanning a signal or signals that determine action is needed

decoding: something that, once initiated, reads the inputs and gets the signals into a form that the program can use. So if three digital lines define an input value, decode them into a single number that the program can read.

Then you can use simple programming to compare the input values with the desired values, and take the appropriate action.

The key thing when doing something like this is to map it out before you start to program anything. Flowcharts are one way of doing it. Then test the design on paper. Coding is almost the last step, when you have something that is fairly complete and you have spotted the big snags before spending time on the detail.

I would also say that for clarity of programming, try to use constructs like do/loop; select/case; and if/then/else with everything defined as symbols with useful names and many subroutines for defined tasks, rather than a great kludge of code that jumps all over the place and won't make sense six months later.

I'm sure that many will help you as you get into the details, this is a good forum and you will get much support.
Thank's for the reply.

Yes I've purchased a handful of optoisolator boards to cover the signal conditioning. The tool turret motor driver board (L298N based) is also purchased as a ready made board and the subroutines for driving the DC motor are ready and tested. But you are right, a flowchart will make it easier to determine how to approach the entitity.
 

nyhoyn

New Member
Been trying to make a code for reading inputs and converting into decimal values, but I can't get it working properly, can anybody give some advice on what is wrong?
Code:
[color=Blue]symbol [/color][color=Purple]position [/color][color=DarkCyan]= [/color][color=Purple]b1[/color]
[color=Blue]symbol [/color][color=Purple]opto1 [/color][color=DarkCyan]= [/color][color=Purple]pinC.6[/color]
[color=Blue]symbol [/color][color=Purple]opto2 [/color][color=DarkCyan]= [/color][color=Purple]pinC.5[/color]
[color=Blue]symbol [/color][color=Purple]opto3 [/color][color=DarkCyan]= [/color][color=Purple]pinC.4[/color]
[color=Blue]symbol [/color][color=Purple]opto4 [/color][color=DarkCyan]= [/color][color=Purple]pinC.3
position [/color][color=DarkCyan]= [/color][color=Navy]0[/color]

[color=Black]main:[/color]
[color=Blue]if [/color][color=Purple]opto1[/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=DarkCyan]and [/color][color=Purple]opto2[/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=DarkCyan]and [/color][color=Purple]opto3[/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=DarkCyan]and [/color][color=Purple]opto4[/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=Blue]then let [/color][color=Purple]position[/color][color=DarkCyan]=[/color][color=Navy]1[/color]
[color=Blue]else if [/color][color=Purple]opto1[/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=DarkCyan]and [/color][color=Purple]opto2[/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=DarkCyan]and [/color][color=Purple]opto3[/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=DarkCyan]and [/color][color=Purple]opto4[/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=Blue]then let [/color][color=Purple]position[/color][color=DarkCyan]=[/color][color=Navy]2[/color]
[color=Blue]else if [/color][color=Purple]opto1[/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=DarkCyan]and [/color][color=Purple]opto2[/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=DarkCyan]and [/color][color=Purple]opto3[/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=DarkCyan]and [/color][color=Purple]opto4[/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]then let [/color][color=Purple]position[/color][color=DarkCyan]=[/color][color=Navy]3[/color]
[color=Blue]else if [/color][color=Purple]opto1[/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=DarkCyan]and [/color][color=Purple]opto2[/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=DarkCyan]and [/color][color=Purple]opto3[/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=DarkCyan]and [/color][color=Purple]opto4[/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]then let [/color][color=Purple]position[/color][color=DarkCyan]=[/color][color=Navy]4[/color]
[color=Blue]else if [/color][color=Purple]opto1[/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=DarkCyan]and [/color][color=Purple]opto2[/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=DarkCyan]and [/color][color=Purple]opto3[/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=DarkCyan]and [/color][color=Purple]opto4[/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]then let [/color][color=Purple]position[/color][color=DarkCyan]=[/color][color=Navy]5[/color]
[color=Blue]else if [/color][color=Purple]opto1[/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=DarkCyan]and [/color][color=Purple]opto2[/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=DarkCyan]and [/color][color=Purple]opto3[/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=DarkCyan]and [/color][color=Purple]opto4[/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]then let [/color][color=Purple]position[/color][color=DarkCyan]=[/color][color=Navy]6[/color]
[color=Blue]else if [/color][color=Purple]opto1[/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=DarkCyan]and [/color][color=Purple]opto2[/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=DarkCyan]and [/color][color=Purple]opto3[/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=DarkCyan]and [/color][color=Purple]opto4[/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]then let [/color][color=Purple]position[/color][color=DarkCyan]=[/color][color=Navy]7[/color]
[color=Blue]else if [/color][color=Purple]opto1[/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=DarkCyan]and [/color][color=Purple]opto2[/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=DarkCyan]and [/color][color=Purple]opto3[/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=DarkCyan]and [/color][color=Purple]opto4[/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=Blue]then let [/color][color=Purple]position[/color][color=DarkCyan]=[/color][color=Navy]8[/color]
[color=Blue]end if


sertxd ([/color][color=Black]#[/color][color=Purple]position[/color][color=Black],[/color][color=Blue]CR[/color][color=Black],[/color][color=Blue]LF)

goto [/color][color=Black]main[/color]
Thanks
 

hippy

Technical Support
Staff member
I would start by simply checking the input bits match what you expect for each tool position ...

main:
serxd( #opto1, #opto2, #opto3, #opto4, CR, LF )
pause 500
goto main

The most likely issue may be that your inputs are inverted to what would be expected or the pin ordering is not quite right.

I would also rename "Tool 8" as "Tool 0" in the documentation. That will make things easier in the long term.
 

geoff07

Senior Member
If you use b0 (which is also bit0-bit7) and set bit0 equal to the state of tool 0 (hence hippy's comment), and the others in sequence, then b0 will have a decimal value equal to the binary representation set by the bits. So you don't actually have to do anything to convert from binary to decimal this way. You then need to identify the decimal values that mean something, and then use a select/case statement to pick them out and take action, viz:
Code:
symbol selected_tool = b0  'or whatever
select selected_tool
   case 0: call deal_with_zero
   case 1: call deal_With_one
   ...
   else: call tool_error
endselect
In place of the 'case 0' etc. you could define other symbols:

symbol special_tool_thingy = 1

case special_tool_thingy: call deal_with_special_tool_thingy

It all helps make the code understandable later. The call statements mean you can put the detail elsewhere in a subroutine without cluttering the main logic.
 

nyhoyn

New Member
Hi,
I've got the code problem solved, it was actually a software issue, when I degraded from PE6 editor to PE5.6 version the code started to work as intended, I've only been testing in simulation state. So far so good. Thanks for the help this far, I'm sure that I'll have more questions as the project moves on. :)
 

hippy

Technical Support
Staff member
I've got the code problem solved, it was actually a software issue, when I degraded from PE6 editor to PE5.6 version the code started to work as intended, I've only been testing in simulation state.
Please can you post your code ( if changed from above ) and detail how the problem exhibited itself; what results you were you getting / not getting compared to expectations. That way we can assess where any issue lies and tweak things if necessary.
 

nyhoyn

New Member
Please can you post your code ( if changed from above ) and detail how the problem exhibited itself; what results you were you getting / not getting compared to expectations. That way we can assess where any issue lies and tweak things if necessary.
The code is the same as above except for that I followed your advice about the last tool (0 instead of 8), I also added the posting of the "opto1...4" values to the terminal window. Before starting simulation I marked certain inputs "high" (yellow) in order to get the desired position number, for example if I wanted position 3 I would set 1101 on the pins for "Opto1...4". If I checked the "Opto1...4" by moving the mouse pointer on the "Opto1...4" I could se that they where in the correct state so "position" value should be 3, but the terminal window showing 5 as value for "position" which also could be confirmed by moving the mouse pointer to the "Position". The problem was with position 3...6 by some reason. After degrading to PE5.6 It works as intended (simulation anyway) and I've the code is near completed, altough it isn't any piece of art. I'll post it later.

Here is the complete code

Code:
' ========== Huvudprogram.bas =========
' Program runs on the 20M2 microcontroller
' It handles a L298N based motor-direction control for a lathe tool turret.

' === Constants ===

 symbol enable = B.1	'PWM output	
 symbol OK = B.6      	'tool change complete output
   
 ' Variables ===
 symbol CNCTool = b1	' Position value CNC input
 symbol O148 = pinC.2 	' CNC input bit 0
 symbol O149 = pinC.1 	' CNC input bit 1  
 symbol O150 = pinC.0 	' CNC input bit 2
 symbol M6 = PinB.5   	' Tool change request
 symbol position = b0 	' Tool turret position
 symbol opto1 = pinC.6	' Tool turret position signal
 symbol opto2 = pinC.5	' Tool turret position signal
 symbol opto3 = pinC.4	' Tool turret position signal
 symbol opto4 = pinC.3  ' Tool turret position signal
 

' === Directives ===

   #com 11 			' specify com port
   #picaxe 20M2 		' specify processor 
   #terminal 9600		' open terminal window




low enable 			' make B.1 an output
pwmout enable, 99, 0 	' set up PWM (0% duty)

' === Begin Main Program ===

main:				' listen for M6 (Toolchange request)

low OK 			' make B.6 an output
				
Do while M6 = 0
 
 low enable				' setup for Hold			
 high B.2 '				' direction backwards, hold against dowel pin
 sertxd ("Hold", CR, LF)
 pwmduty enable, 300 		' 75% 

loop 
 

 Toolinput: 								' read the cnc-input for toolnumber
 
 if O148= 0 and O149= 0 and O150= 1 then let CNCTool=1 	' tool 1
 else if O148= 0 and O149= 1 and O150= 0 then let CNCTool=2 ' tool 2
 else if O148= 0 and O149= 1 and O150= 1 then let CNCTool=3 ' tool 3
 else if O148= 1 and O149= 0 and O150= 0 then let CNCTool=4 ' tool 4 
 else if O148= 1 and O149= 0 and O150= 1 then let CNCTool=5 ' tool 5
 else if O148= 1 and O149= 1 and O150= 0 then let CNCTool=6 ' tool 6
 else if O148= 1 and O149= 1 and O150= 1 then let CNCTool=7 ' tool 7
 else if O148= 0 and O149= 0 and O150= 0 then let CNCTool=0 ' tool 8
 end if
 
 sertxd (#CNCTool,CR,LF)
 sertxd( #O148, #O149, #O150,CR, LF )
 pause 1000
 
 if CNCTool=position then goto Tool_ok				' check if tool input is same as current tool
 goto fwd 									' Tool turret need to be rotated

 
 fwd: 									' check for right position
 Do while CNCTool <> position
 high B.0 									' setup for forward rotation
 low B.2 									' setup for forward rotation
 sertxd ("Forward", CR, LF)
 pwmduty enable, 400 							' 100% "full speed"
 
 '==== monitor toolturret pos.output ====
 
 if opto1= 1 and opto2= 1 and opto3= 1 and opto4= 0 then let position=1 
 else if opto1= 1 and opto2= 1 and opto3= 0 and opto4= 0 then let position=2
 else if opto1= 1 and opto2= 1 and opto3= 0 and opto4= 1 then let position=3
 else if opto1= 1 and opto2= 0 and opto3= 0 and opto4= 1 then let position=4
 else if opto1= 1 and opto2= 0 and opto3= 1 and opto4= 1 then let position=5
 else if opto1= 0 and opto2= 0 and opto3= 1 and opto4= 1 then let position=6
 else if opto1= 0 and opto2= 1 and opto3= 1 and opto4= 1 then let position=7
 else if opto1= 0 and opto2= 1 and opto3= 1 and opto4= 0 then let position=0
 end if
 
 sertxd (#position,CR,LF)
 pause 1000
 loop
 goto stopp 	'stop 


 stopp: 		'stop
 low enable
 low B.0 		'disable output
 low B.2 		'disable output
 wait 1
 goto bak1
 
 
 bak1: 		' go in reverse until stalls against dowel pin
 low B.0 		' setup for reverse
 high B.2
 sertxd ("Reverse", CR, LF)
 pwmduty enable, 400 ' 100%
 wait 1		' time to reverse and stall against dovel pin, to be replaced with current sensor on L298N
 goto Tool_OK
 
 
 Tool_OK:		' Tool change completed, back to monitoring M6
 High OK
 sertxd (#OK, CR, LF)
 pause 500
 goto main
 
Last edited:
Top