New jump design

marzan

Senior Member
#1
Hello everyone. I have been working on a project for years, and have changed the mechanical side of my idea several times, trying to get the simplest way of getting them working with the least amount of parts. I have gone back to the original idea of using stepper motors, because they are now so cheap and so are the drivers, thanks to 3d printers. I have written some code. some of it works, some of it dosen`t. I was hoping you could tell me where I have gone wrong.
When it is switched on for the first time, you have to press the white button to calibrate the height so the stepper knows where it is. This is where I want to send the calibrated value to non volatile memory (which dosen`t work). after setting the height it reverts back to the main program. I want to set an interrupt flag so that if the battery is disconnected for any reason and powers down the program sends the actual height value to NVM, and then sets off the fault light (all of which also dosent work)
I am using 20m`s, which I have quite a few of (yeah I know, use the m2`s) so I am also running out of code space. I was hoping to implement some sort of battery check setup with the 3 output pins which are connected to NPN transistors, but I have run out of room.
code in next post due to 1000 character limit.

Any ideas?
Thanks in advance
Marz.
 

marzan

Senior Member
#2
Here is the code:
Code:
SYMBOL pulse_actual = W0
symbol height_to = b3
symbol pulse_to = W2
symbol pulsemove = w3
symbol fault = b.0
symbol bzzr = b.1
symbol out1 = b.2
symbol out2 = b.3
symbol out3 = b.4
symbol step_pulse = b.6
symbol direction = b.5
symbol enabl = b.7
symbol voltcheck = c.1
symbol inpt1 = c.4
symbol inpt2 = c.5
symbol inpt3 = c.6
symbol rfchip = c.7


setfreq M8
high enabl         ;set driver module to no current
low direction        ;set direction to low to stop possible floating pin
wait 5
high fault         ;check fault led
wait 1
low fault
if pin6 = 1 then goto set_height     ;check for menu change to calibrate mode
SETINT %00000000,%00010000         ;set interrupt to write to memory the position of the stepper if the power shuts down
main:
 high enabl
 read 254,pulse_actual
 serin c.7, n1200,("abc"),height_to           ;(n2400_16 20m2 freq16mhz)    recieve position info from wireless remote
 high bzzr          ;signal recieved check
 pause 10
 low bzzr
 select case height_to         ;set pulse position to move to
  case 1 pulse_to = 5000
  case 2 pulse_to = 10000
  case 3 pulse_to = 15000
  case 4 pulse_to = 20000
  case 5 pulse_to = 25000
  else goto main
 Endselect
  if pulse_to = pulse_actual then goto main        
  if pulse_to > pulse_actual then goto Go_up ;decide to go up or down
;go_down:
 high direction      
 pulsemove =  pulse_actual - pulse_to   ;calculate how far to move
 low enabl        ;turn on current to module
 pause 30
 for w5 = 1 to pulsemove      ;move to new position
  pulsout step_pulse,1
  dec pulse_actual        ;keep track of position
 next
 write 254,pulse_actual      ;write position to NVM
 goto main

go_up:
 low direction
 pulsemove = pulse_to - pulse_actual     ;decide how far to move
 low enabl
 pause 30
 for w5 = 1 to pulsemove      ;move to new position  
  pulsout step_pulse,1
  inc pulse_actual        ;keep track of position
 next
 write 254,pulse_actual      ;write position to NVM
 goto main          ;go back and wait for new height

set_height:  
 for b12 = 1 to 5             ;audible reminder of menu choice
  high bzzr
  pause 10
  low bzzr
  pause 500
 next
 wait 2
 low enabl
 do
    let b13 = pinsc & %01110000                  ;input trigger value masking unused pins
          ;use yellow and blue buttons to move up or down
          ;use white button to set height at 100mm and then return to main program
         
   select case b13        
    case 16  low direction :pulsout step_pulse,1 : pause 4 ;  (yellow)            
    case 32  high direction: pulsout step_pulse,1 : pause 4 ; (blue)    
    case 64  goto Height_set ; (white)
   endselect
 loop
Height_set:
 pulse_actual = 5000      ;set height to 100mm
 write 254, pulse_actual      ;write starting height to NVM
 goto main
interrupt:
     
               WRITE 254, pulse_actual    ;on loss of power write position to NVM
     high fault
               goto main
/[code]
 

Aries

New Member
#3
I've not looked at your code in detail, but your statement
Code:
write 254, pulse_actual
only writes b0, not w0. You need the WORD keyword to write the whole thing (and ditto for read)
 

hippy

Technical Support
Staff member
#4
Using your SELECT CASE uses 243 bytes -
Code:
select case height_to
  case 1 pulse_to = 5000
  case 2 pulse_to = 10000
  case 3 pulse_to = 15000
  case 4 pulse_to = 20000
  case 5 pulse_to = 25000
  else goto main
Endselect
That can be reduced to 204 with -
Code:
pulse_to = 0
lookup height_to,(0,5000,10000,15000,2000,25000), pulse_to
if pulse_to = 0 then main
And to 195 with -
Code:
If height_to < 1 or height_to > 5 then main
pulse_to = height_to * 5000
That saves 20% of the memory you were using.

You can save another 1.5% by using EEPROM location 0 rather than 254.
 

hippy

Technical Support
Staff member
#5
There's also another 2% which can be saved by changing ...
Code:
go_up:
 low direction
 pulsemove = pulse_to - pulse_actual     ;decide how far to move
 low enabl
 pause 30
 for w5 = 1 to pulsemove      ;move to new position  
  pulsout step_pulse,1
  inc pulse_actual        ;keep track of position
 next
to
Code:
go_up:
 low direction
 low enabl
 pause 30
 do while pulse_actual < pulse_to
   pulsout step_pulse,1
   inc pulse_actual
 loop
You can do similar with the 'go_down' routine and probably even merge the two together. That should give over 25%, possibly 30% saving against what you started with.

Note none of these optimisations have been tested.
 

marzan

Senior Member
#6
Thanks @hippy. I made the changes and it works well.
You can save another 1.5% by using EEPROM location 0 rather than 254. /
I thought I had to use the upper end of the memory or it will clash with the uploaded program? Not that it seems to work. Is it as @Aries said? Is there a read write WORD function that I haven't grasped?
 

hippy

Technical Support
Staff member
#7
I thought I had to use the upper end of the memory or it will clash with the uploaded program?
As you are using a 20M then program code and EEPROM share the same memory. The addresses are however numbered in opposite directions to avoid clashes; EEPROM 0 is the last program code address.

So using 254 actually causes a problem, would overwrite the second location of program memory. If you had added an "EEPROM 254,(0)" command, to let the compiler know you were using that location and pre-initialising it, that would have informed you of the clash, that the memory had been used by the program code.

Is there a read write WORD function that I haven't grasped?
If reading and writing word variable one would usually use 'READ 0, WORD <var>' and 'WRITE 0, WORD <var>' commands.
 

marzan

Senior Member
#8
Thank you @hippy for your help. I got it all to fit using every byte.
Code:
SYMBOL pulse_actual = W0
symbol pulse_to = W1
symbol pulsemove = w2
symbol height_to = b6
symbol battcheck = b7
symbol calbttns = b8
symbol fault = b.0
symbol bzzr = b.1
symbol out1 = b.2
symbol out2 = b.3
symbol out3 = b.4
symbol direction = b.5
symbol step_pulse = b.6
symbol enabl = b.7
symbol voltcheck = c.1
symbol inpt1 = c.4
symbol inpt2 = c.5
symbol inpt3 = c.6
symbol rfchip = c.7


 setfreq M8
 eeprom 0,(0)
 low bzzr
 high enabl
 SETINT %00000000,%01000000         ;set interrupt to write to memory the position of the stepper if the power shuts down
 high fault
 wait 1
 low fault
 high b.2,3,4
 wait 5
 low b.2,3,4
  if pin6 = 1 then goto set_height
main:
 low bzzr
 readADC 1, battcheck
  if battcheck < 200 then high b.2
  elseif battcheck >199 and battcheck <220 then high b.3
  elseif battcheck >219 then high b.4
  endif
 high enabl
 read 0, word pulse_actual
 serin c.7, n1200,("abc"),height_to           ;serin c.7, n2400_16,("abc"),height_to(20m2 freq16mhz)recieve position info from wireless remote
 high bzzr        ;signal recieved check
 pause 10
 low bzzr
 pulse_to = 0
 lookup height_to,(0,5000,15200,25400,35600,45800,56000,4000), pulse_to
  if pulse_to = 0 then main
  if pulse_to = pulse_actual then goto main        
  if pulse_to > pulse_actual then goto Go_up ;decide to go up or down
;go_down:
 low direction
 pause 50        
 pulsemove =  pulse_actual - pulse_to   ;decide how far to move
 low enabl
 pause 50
 for w5 = 1 to pulsemove      ;move to new position
  pulsout step_pulse,1
  dec pulse_actual        ;keep track of position
 next
  WRITE 0, word pulse_actual
 goto main

go_up:
 high direction
 low enabl
  pause 30
  do while pulse_actual < pulse_to
     pulsout step_pulse,1
     inc pulse_actual
  loop
  WRITE 0, word pulse_actual
 goto main          ;go back and wait for new height

set_height:  
 for b12 = 1 to 5
  high bzzr
  pause 10
  low bzzr
  pause 500
 next
 wait 2
 low enabl
 do
    let calbttns = pinsc & %01110000                  ;input trigger value masking unused pins
   select case calbttns        
    case 16  low direction :pulsout step_pulse,1 ; (yellow)            
    case 32  high direction: pulsout step_pulse,1; (blue)    
    case 64  goto Height_set ; (white)
   endselect
 loop
Height_set:
 pulse_actual = 5000
 
 
 
 write 0, word pulse_actual  
 goto main
interrupt:
     
               WRITE 0, word pulse_actual
     high fault
               goto main   
/[code]
 
Top