critique my first attemp please

Rbeckett

Member
Here is my first attempt at micro controller programming and it seems to be very slow. I realize it is incomplete at the very end, but getting there seems to take excessively long even when overclocked at 8MHZ. I am using analog inputs on ADC0 and 1 and a digital proximity sensor switch tied to V+ on input 6 with an interrupt routine on pin 7 . The code is too wide to use the code brackets, so I have attatched it as a .BAS file so it can be read in PE. It is fairly long, but feel free to make any recomendations that you feel would improve the execution speed. TIA for any assistance. Remember this is my first attemp at programming, so be gentle.
Bob:D
View attachment AVTHC coding at 8mhz change8.bas
 

inglewoodpete

Senior Member
I've had a look at the code and I can say it's good to see someone putting lots of comments in their code. Some comments are a bit superfluous or verbose for me but generally good.

However, my biggest problem is getting my head around what it does. I can see "arc", "torch" and "pierce", which to me suggest some sort of plasma cutter. If so, what are you trying to sense and drive with the PICAXE?
 

westaust55

Moderator
It is (as you say) a fairly long piece of code to expect someone to anaylse fully.

Firstly, great to see a lot of comments, but . . . .
some are excessive and uneccessary. For example
goto track ; return to track​

Others are incorrect. For example:
under the routine downslow:
return ; return to main​
It acutally returns to the calling part which itself are a subroutines/sections such as SET: and CUT:


In terms of program flow:

In the Main: program you have a call "gosub track"
but there is not RETURN command at the end of Track:
so the program will eventually fall through Track: into Upfast:

You have the line:
setint %10000000,%10000000​
scattered through the program
In reality one at the start and then at the end of the Interrupt: routine will suffice.

Your have SETFREQ m8 at the start and also under the label Set:.
Unless you change the clock speed there is no need to repeat the command.


Near the top you have:
symbol Del=1 ; set delay to 1 microsecond​

then you have many many lines with:
pins = %0010 : pause del​
The pins configurations changes - it is the delay/pause that is of interest


PAUSE values are in 1 milli second not 1 microsecond increments.
roughly, each BSIC comamnds takes around 250 microseconds (the nature of interpreted BASIC programming). If you really want microsecond type pauses set Del = 0 and try.
Later remove all these PAUSE del commands.

Without looking at the program flow, you also have a lot of other pasues such as 250ms, 1000ms, 1500ms, 2000ms, etc.

Taking into consideration the above mentioned approx 0.25ms duration per program command and all the pasues it may be no wonder that your program is quite slow.

Suggest you revisit the delays in the first instance and also look at subroutines to make sure they are returning correctly.

Try it out in the Programming Editor simulator and see if the program flow is what you expect.
 

Rbeckett

Member
Thanks for the help guys, I appreciate it. This is my first attempt at microcontrollers, and what I have done is read and re-read the manuals and apply what I "understood". My lack of understanding for items such as Milli/Micro second will improve. The device is a concept, but has definite inputs, and the control E-stop. Do you think I would be better served to use an M2 chip with a higher pin count and higher freq? That would give me the ability to add features the 18X does not have. When I originally began, I had no clue (not that I have a clue now), I took my best "uninformed guess" to start.

Westaust55:
Thank you for you time and input. You are correct on every one of your observations and I will adjust/remove them. I copied code from threads and manuals. The stepper code was from a thread and the delay included. I have lowered the delays, but left them on the assumption that they were there to prevent damage to the drivers from back emf. On the device I will include diodes, as well as opto isolation. While running the sim, the setint command was erratic so I duplicated it as a safety net so I could stop processing rapidly. I will remove them as possible, and still keep E stop. I will remove the multiple set freq commands. I was cutting and pasting and caused that.

Inglewoodpete:
Thank you also for taking the time to look. I am a forgetfull (old and disabled) guy and comments tell me what I was doing. I use comments to tell me what's happening. Some comments were not updated after errors, sloppy note taking on my part, my Appologies.
This device is (hopefully) designed to act as a stand alone Z axis control on a CNC Plasma Table. The plan is to closely follow a sheet or plate and maintain an optimal .063 +or- .005 inch between the tip and plate. Pierce height is .125 In, depending on metal thickness and composition. This allows molten metal to be blown clear when starting a new cut from within or on an edge. Orifice damage occurs from improper pierces and affect edge quality and component life. Control means less clean up prior to the next operation, which saves time, and money for wheels and consumables. The consumables average 25.00 a pop, and can be hurt on the first use. So it wont take long for cost saving if I can improve that 50%.

Thanks for all of the help and assistance from everyone, and I appologize for the length of the posts. I am still very new to this, and held back till I had something I felt would not waste the memberships time correcting too many stupid mistakes. I am sure ther are still a few, but I am minimizing them as possible to avoid wasting your valuable time.
Bob
 

inglewoodpete

Senior Member
Bob,

When I considered your work, I formed the conclusion that you may have written some code but not actually tested it yet.

When creating software that will interface a PICAXE, or any microcontroller for that matter, to a motor-driven real world mechanical object there are many variables that you will not be aware of in the design stage. Eg inertia

Bitter experience has taught me that while an overall software design is necessary, attempting to write the entire program without working out the detailed interfacing/feedback first. There are several methods of doing the overall design: the flowchart is possibly the most easily understood.

If you have not already done so, I suggest you start small: write the code to read the input or feedback mechanism with outputs via "Debug" or "SerTxd" staements. When satisfied that this performs to your specs, then add the code to drive and then control a motor. When this works, complete the feedback loop so the PICAXE can monitor its own attempts or the mechanical positioning and provide correction.

Once when have a basic feedback loop operational, you can add further functionality.

Peter
 

Rbeckett

Member
Quick update on the slow program execution issue. Changed to a 40X2 chip at 64 MHZ and it runs appreciably quicker. Still making the suggested changes from the esteemed membership (been busy with health issues) and learning the new pin naming/direction conventions for a larger higher powered chip (18X to 40X2), but I am continuing to make good progress... Thanks tons guys (Inglewood, and Westy), I really appreciate your extremely valuable help and guidance!!!!
Bob
 

MPep

Senior Member
Having had a look at your code, I notice that DOWNSLOW is the same as TRACKDOWNSLOW. Probably others also.
Suggest you re-use what you already have.

Remove all the references to SETINT and only setup once BEFORE going past the MAIN label.

I have re-written what you had but modified to, what I think, is required to do the job.

Code:
#Picaxe18X                    ; names project chip design/pin count and mod level

Symbol Red=4                ; rename 4 red (ready)
symbol Yellow=5                ; rename 5 yellow (arc detected)
symbol Blue=6                ; rename 6 blue (OK to move)
symbol WHITE=7                ; rename 7 white (Plate sensed)
symbol Del=1                ; set delay to 1 microsecond
symbol Target=b0                ; defines b0 as target arc voltage from adc 0
symbol Actual=b1                ; defines b1 as actaul arc voltage from adc 1
symbol Counter=b2                ; defines step counter limits
symbol Whisker=6                ; defines microswitch on input pin 6

setint %10000000,%10000000        ; set up emergency stop routine
    
Main:                        ; make label for start of main program
    setfreq m8                ; set chip frequency to 8MHZ
    gosub test                ; perform LED test on power up
    let b2=0                ; resets counter to zero
    gosub set                ; performs set to find plate 
    let pins= %10000000        ; turns off ready and turns on plate sensed 
    gosub pierce            ; moves torch to pierce height
    pause 1500                ; pauses 1.5 seconds for torch to pierce metal
    gosub cut                ; moves torch to cut height
    pause 500                ; allows torch to stop and stabilize at cut and send command
    let pins= %01000000        ; turn off plate sensed,turn on OK to move light, send OK to controller
    gosub track                ; keeps torch at proper height while moving on X and Y axis
    end                    ; end of AVTHC program

test:                        ; make label for led test
    let pins= %11110000        ; turn on red, yellow, blue and white led
    pause 2000                ; wait 2 second
    let pins= %00000000        ; turn off all LED's
    pause 1000                ; wait 1 second
    let pins= %00010000        ; turn on"ready" led. system ready for height control
    pause 1000                ; wait 1 second before returning to program
    return                 ; return to main program
    
set:                        ; make label for setting torch to pierce height    
    setfreq m8                ; set operation to up 8 MHZ from default 4 MHZ
    for b2=1 to 5            ; set counter for XXX (VARIABLE NUMBER) 
    if pin6=1 then exit        ; looks for whisker switch to contact plate
    gosub upfast:            ; use sub routine to move torch up in full step mode (quickly)
    next                    ; moves to next command in sequence
    pause 250                ; pause 1/4 seconds for motor to completely stop before next command
    for b2=1to 3            ; sets  new variable for counter to detect plate
    gosub downslow            ; moves torch toward plate slowly
    if pin6=1 then exit        ; looks for whisker switch to contact plate
    gosub downslow            ; continues to move torch down slowly
    next                    ; moves to next command in sequence                                                    
    pins=%10010000             ; checks whisker switch to prevent nozzle damage from strike
    pause 1000                ; pause 1 second to allow stepper to come to a complete stop
    goto test                 ; return to main program at operate,
    
pierce:    

    for b2= 1 to 5            ; set b2 for 0.165 pierce height
    gosub upslow            ; moves torch up 17 steps slowly
    next                    ; moves to next command in sequence
    pause 10                ; allows motor to completely stop
    pins= %00010000            ; turns off plate sensed led and turns on ready to fire led
    pins= %00100000            ; arc transfered during pierce
    goto cut                 ;return to main program

cut:    
    for b2= 1 to 3            ; sets counter to move torch down to cut height
    gosub downslow            ; moves torch from pierce height to cut height
    next                    ; moves to next command in sequence 
    pins= %01000000            ; turns on OK to move led and sends command OK to move on x,y axis
    goto track                 ; return to main program

Track:
    do                    ; sets up continous adjustment loop within program
    let b2=0                ; sets counter to 0
    readadc 0, b0             ; read adc 0 to memory
    readadc 1, b1             ; read adc 1 to memory
    if b0<b1 then downslow        ; set up comparison for plate tracking during a cut
    if b1<b0 then upslow        ; continue comparison of actual and target arc volts
    if b0=b1 then track        ; finish logic loop to track torch to plate height
    loop while b1>1             ; puts track into continous loop, b1=o ends program

upfast:                     ; make label to move torch up in full step mode
    pins = %1000 : pause del     ; 1
    pins = %0010 : pause del     ; 2
    pins = %0100 : pause del     ; 3
    pins = %0001 : pause del     ; 4
    return                ; return to main 
    
downfast:                     ; make label to move torch down in full step mode
    pins = %0001 : pause del    ; 4 
    pins = %0100 : pause del    ; 3
    pins = %0010 : pause del    ; 2
    pins = %1000 : pause del    ; 1
    return                ; return to main
    
upslow:                    ; make label to Move torch up in 1/2 step mode
    pins = %1000 : pause del    ; 1
    pins = %1010 : pause del    ; 1 and 2
    pins = %0010 : pause del    ; 2
    pins = %0110 : pause del     ; 2 and 3
    pins = %0100 : pause del     ; 3
    pins = %1100 : pause del     ; 3 and 4
    pins = %0001 : pause del     ; 4 
    pins = %1001 : pause del     ; 1 and 2
    return                ; return to main
    
downslow:                     ; make label to move torch down in 1/2 step mode
    pins = %1001  : pause del     ; 1 and 2
    pins = %0001  : pause del     ; 4
    pins = %1100  : pause del     ; 3 and 4
    pins = %0100  : pause del     ; 3
    pins = %0110  : pause del     ; 2 and 3
    pins = %0010  : pause del     ; 2
    pins = %1010  : pause del     ; 1 and 2
    pins = %1000  : pause del     ; 1
    return                ; return to main

interrupt:                    ; make label for emergency stop routine.
    do                    ; stop movement and flash LEDS
    let pins= %11110000        ; flash leds to indicate fault
    pause 1                ; wait 1 second
    let pins= %00000000        ; flash leds off
    loop                    ; loop for flash
    if pin7=1 then interrupt    ; keep in fault loop till fault reset and cleared
    pause 2000                ; pauses 2 seconds before resuming programming
    setint %10000000, %10000000    ; resets the interupt command
end
Does this do what you expect?
Uncertain what exactly is meant to happen here, so am only taking a stab in the dark. :D

MPep.
 
Top