24v Servo ( well its Getting There)

xtech007

Senior Member
Hi all !!
Many thanks to westaust55 for helping me many times specially on the I2C part of the coding, also many of you who where very helpful.

As promised here are the latest news:

1-tested the code with the motor Installed
Here are the actions that took Place: when pot 1 (b1) was moved in either direction the motor would turn in that direction and match (b1) travel or distance. Noted if I physically try to move it out of the position once it had reached b1 to either direction it will go back to match b1 position. COOL!!!

When b1 is released, no matter what previous position it was before it will center or follow b1. Great!!!!! it self centers itself !!

2 Errors I noted (glitches)
A-in the code its specified that IF b2 > 40 AND b2 < 215 THEN Check
problem: when b2 has reached end of travel say 40 or 215 and I try to
move it out that position there is no action no matter in what direction
b1 is moved.
B- when b1 is centered, depending if it was on the right or left b1 has a different center position exp:I move joystick to the left ad let the joystick go it moves to the center position even if I try multiple times it goes to the same center position. But if from that center position I move the joystick center position to the right and let it go now it has a different center position. Its shy by maybe 1-2 inches from matching the other.

Here is the code Used:
Code:
'############################################
'Picaxe 28X1                                                                       
'Using a 10K Potentiometer for Input                                      
'DC Motor 24V and an MD03 as H-bridge                              
'############################################


'###################  
'SETUP  
'###################  

Symbol Right=1
Symbol Left=2

Symbol M_Stop=0
Symbol Speed_1=100
Symbol Speed_2=200
Symbol Speed_0=0
SYMBOL proximity = b3
SYMBOL tolerance = 5

HI2CSETUP i2cmaster, %10110000, i2cfast, i2cbyte  ; %10110000 = $B0 = default MD03 Slave Address


'###################  
'MAIN LOOP
'###################  

Main:
        READADC 0, b1				      	' read value on ADC0 into variable b1 from pot on steering wheel = desired position
        READADC 1, b2  	      	  		      ' read value on ADC1 into variable b2 from pot = actual position



        IF b2 > 40 AND b2 < 215 THEN Check            ' Not at either end stop
        
        GOSUB Motor_stop  					' end travel left or right so STOP
	  GOTO Main

Check:	        
        IF  b1 >  b2 THEN
          proximity = b1 - b2
        ELSE
          proximity = b2 - b1
        ENDIF

        IF proximity < tolerance THEN 
          GOSUB Motor_stop                            ' stop motor as actual position = selected +/- tolerance
        
        ELSEIF b1 > b2 THEN
          GOSUB turn_left                             ' If steering wheel is further left than feedback (b2) then Go Left
        
        ELSEIF B1 < b2 THEN
          GOSUB turn_right  		                  ' If steering wheel is further right than feedback (b2) thenGo Right
        
        ENDIF

        GOTO Main
 

'###################  
'SUBROUTINES
'###################  

Turn_right:
        HI2COUT [%10110000], 0, (Right)
        HI2COUT 2, (Speed_1)
        RETURN

Turn_left:
        HI2COUT [%10110000], 0, (Left)
        HI2COUT 2, (Speed_1)
        RETURN

Motor_stop:
        HI2COUT [%10110000], 0, (M_Stop)
        HI2COUT 2, (Speed_0)
        RETURN
any help will be Appreciated. Thanks!!!!!!!
 

westaust55

Moderator
I'll keep waiting and testing.
My ADSL modem failed last night :mad: but now up and running on a new one :)
Have a few other projects (solar panels on my camper trailer) to look at first (between heavy rain :rolleyes:). Will see if I can get to your post later today . . .
 

BCJKiwi

Senior Member
The problem might be here;
Code:
        IF b2 > 40 AND b2 < 215 THEN Check            ' Not at either end stop
        
        GOSUB Motor_stop       ' end travel left or right so STOP
   GOTO Main
Once either end limit is reached the code will always bypass the Check subroutine as it is still at one end or the other so will constantly loop back to Main: via Motor_stop.

So you need a way to break ot of that loop when you command a new position.

I wonder if you actually need that bit of code at all as the Check Subroutine with it's test for proximity and tolerance should not try to command past the stops. You could ensure that by putting limits on the readadc using Max and Min (to match the stop values) so the steering input cannot go past the actual stop.

something like (UNTESTED);
Code:
Main:
        READADC 0, b1           ' read value on ADC0 into variable b1 from pot on steering wheel = desired position
   b1 = b1 min 40 max 215
        READADC 1, b2                    ' read value on ADC1 into variable b2 from pot = actual position
 
 ;       IF b2 > 40 AND b2 < 215 THEN Check            ' Not at either end stop
        
 ;       GOSUB Motor_stop       ' end travel left or right so STOP
 ;  GOTO Main
Check:
 

westaust55

Moderator
xtech007,

Great to see that your project is taking shape.

below is a reworked version of you code that may interest you.
Should solve your mentioned problem (motor stays at end travel limits) and also impliments a two speed operation along the lines that you had defined in your SYMBOL ststements
Symbol Speed_1=100
Symbol Speed_2=200​

As the motor approaches the set position it will slow down before stopping.
It does use two more variables.
I also renamed "tolerance" as "deadband" which is more in line with Servo terminology.
reworked the subroutine so only one needed - in fact you could now incorportate into the main code as a result.

have fun :)

Code:
'############################################
'Picaxe 28X1                                                                       
'Using a 10K Potentiometer for Input                                      
'DC Motor 24V and an MD03 as H-bridge                              
'############################################


'###################  
'SETUP  
'###################  

SYMBOL proximity = b3
SYMBOL speed     = b4
SYMBOL directn   = b5

SYMBOL Right     = 1
SYMBOL Left      = 2

SYMBOL M_Stop    = 0
SYMBOL slow      = 1
SYMBOL fast      = 2

SYMBOL deadband   = 5
SYMBOL slowdown   = 11
SYMBOL speedstep = 100
SYMBOL leftlimit  = 215
SYMBOL rightlimit = 40


HI2CSETUP i2cmaster, %10110000, i2cfast, i2cbyte  ; %10110000 = $B0 = default MD03 Slave Address


'###################  
'MAIN LOOP
'###################  

Main:
        READADC 0, b1				      	' read value on ADC0 into variable b1 from pot on steering wheel = desired position
        READADC 1, b2  	      	  		      ' read value on ADC1 into variable b2 from pot = actual position


Speedset:	        
        IF  b1 >  b2 THEN
          proximity = b1 - b2
        ELSE
          proximity = b2 - b1
        ENDIF

        IF proximity < deadband OR b2 <=rightlimit OR b2 >=leftlimit THEN
          speed = M_Stop
        ELSEIF proximity < slowdown THEN
          speed = slow
        ELSE
          speed = fast
        ENDIF
        
        speed = speed * speedstep
        
DirectionCheck:
        IF speed  = 0 THEN
          directn = M_stop
        ELSEIF b1 > b2 THEN
          directn = left                            ' If steering wheel is further left than feedback (b2)  then Go Left
        ELSE
          directn = right                           ' If steering wheel is further right than feedback (b2) thenGo Right
        ENDIF
        

	  GOSUB Set_Motor
        GOTO Main
 

'###################  
'SUBROUTINES
'###################  

Set_Motor:
        HI2COUT [%10110000], 0, (directn)
        HI2COUT 2, (speed)
        RETURN
 

xtech007

Senior Member
Many thanks for the reply !!

You guys are great.

First I would like to give a hand for the awesome work you guys do in the forum.
Thanks to you all and the helpful feedback I gain so far I was Able to help someone who was in need.

A friend of mine had trouble with his electric wheel chair the controls would sometimes work, well more like never. To repair it was expensive. So I asked him if he would be able to pay for the MD03, so we order a new one. I placed mine and an old arcade controller along with an 28x1 and the code westaust55
walked me through at the very beginning. So now the wheelchair actually moves in the direction (FWD-ReV) when told to and control does not stick.

How cool is that !!!!!!!! :) Thanks..

westaust55 I will definitely try the code tonight, will post results and video..
 
Top