Readtemp Slowing Program

Jarubell

Senior Member
Hello!

Well, I have set out to have analogue inputs to one PICAXE, convert to servo variables, do a little math, and send the results to another PICAXE via a pair of half-duplex RS-485 transceivers. The second PICAXE sends those variables to a AXE033 for now, need to wait on purchasing more parts to continue, but all is well. I figured that I would like to send info back to the first PICAXE, but I'm still thinking about how to "handshake" as suggested by lbenson a while back. So in the meantime, I introduced a DS18B20 to the second PICAXE to display on the AXE033, but I noticed that the program has slowed down, not as responsive as before. "The conversion takes up to 750ms", didn't seem long before I downloaded it to my project!

Is there another faster way to have a temperature reading included to my project? I would also like to add a pressure sensor and a couple discrete inputs as well down the road. Is it the 18M2, should I look into other PICAXEs?

Not sure if there was an easier way to get the results I'm looking for, but I certainly happy with what I have done so far! That being said, I am open to suggestions.

Code:
#PICAXE 18M2
Init: Pause 500

Symbol thrust=w1
Symbol yaw=w2
Symbol active_span=w3 	
Symbol deviation=w4 	
Symbol servo_left=w5
Symbol servo_right=w6
Symbol led_sw=pinB.7    	   
Symbol led_latch=b0
Let led_latch=0

Main:

	High b.2					;enable send Max485 disable receive
	serout c.2,n2400_4,("XYZ",servo_left,servo_right,thrust,yaw,led_latch)
	low b.2 						  ;disable send Max485/ enable receive Max485
	;calculate the x axis of  the joystick to servo valuereadadc c.1,thrust
	readadc c.0,yaw
	
	if led_sw=1 then:gosub Latch:endif
		
	
	if thrust>130 and yaw<128 then:gosub ForwardLeft
		elseif thrust>130 and yaw>127 then:gosub ForwardRight
		elseif thrust<125 and yaw<128 then:gosub ReverseLeft
		elseif thrust<125 and yaw>127 then:gosub ReverseRight
	else
		gosub Neutral
	endif
	goto main

ForwardLeft:
	
	let servo_right=thrust*150/255+75			
	let active_span=servo_right-150*2			
	let deviation=128-yaw*active_span/128
	let servo_left=servo_right-deviation
	
	return

ForwardRight:
	
	let servo_left=thrust*150/255+75
	let active_span=servo_left-150*2
	let deviation= yaw-128*active_span/128
	let servo_right=servo_left-deviation
	
	return

ReverseLeft:

	let servo_right=thrust*150/255+75
	let active_span=150-servo_right*2
	let deviation=128-yaw*active_span/128
	let servo_left=servo_right+deviation
	
	return
	
ReverseRight:

	let servo_left=thrust*150/255+75
	let active_span=150-servo_left*2
	let deviation=yaw-128*active_span/128
	let servo_right=servo_left+deviation
	
	return
	
Neutral:

	let servo_left=128
	let servo_right=128
	
	return

Latch:

	High b.3
	Pause 500
	Do: Loop until Led_sw=0
	low b.3
	led_latch=led_latch+1
	if led_latch>2 then
		let led_latch=0
	endif
	
	return
Code:
#PICAXE 18M2
Init: Pause 500

Serout c.1,n2400,(254,1)
pause 30
serout c.1,n2400,(254,128,"Powering up") 
pause 2000
Serout c.1,n2400,(254,1)
pause 30


Symbol thrust=w1
Symbol yaw=w2
Symbol servo_left=w5
Symbol servo_right=w6
Symbol led_latch=b0
;Symbol Temp=b15

Main:

	Serin c.2,n2400_4,("XYZ"),servo_left,servo_right,thrust,yaw,led_latch
	
	if led_latch=1 then
		high b.3
	elseif led_latch=2 then
		high b.2
		else
			low b.2:low b.3
	endif 	 

	;Readtemp c.0, Temp

	Serout c.1,n2400_4,(254,128,#servo_left,"    ")
	Serout c.1,n2400_4,(254,134,#servo_right,"    ")
	Serout c.1,n2400_4,(254,192,#thrust,"    ")
	Serout c.1,n2400_4,(254,198,#yaw,"    ")
	;Serout c.1,n2400_4,(254,140,#Temp," ")
	
	goto main
Thank you.
 

premelec

Senior Member
I don't know if this might address your issue but the DS18B20 has a pretty long thermal response lag too so you might read it less often.... faster response can be had with low mass PT100, thermistor, TC units with appropriate circuitry and READADC...
 

Jarubell

Senior Member
I happen to have a RTD sitting here beside me, I'll look into it but your suggestion of reading it less often would work. I really do not need it updated that often, even if it was every minute, a 750ms pause then would be acceptable.

Thank you very much.
 

lbenson

Senior Member
I haven't done it, but there are threads about using the "one-wire" capability of the DS18B20 (owin and owout commands) to first tell the DS18B20 to take a reading, return to the program for more processing, and then query the DS18B20 at your convenience as to the result. Search on "owin ds18b20".
 

westaust55

Moderator
Are you using READTEMP or READTEMP12 ?
The 12-but mode takes longer for the DS18B20 to internally do the conversion.

The newer PICAXE parts poll the DS18B20 to allow continuation as fast as possible.

Note that only the X1 and X2 parts have the OWIn and OWOut commands.
 

Jarubell

Senior Member
Tried both.

I'm using 2 18M2, for now I'll just read the temp less often but I will look into the newer Picaxes.

Thanks for the note on OWin/out.
 

Jarubell

Senior Member
Looked at my processors and found one etched 18M2 and the other is 18M2+, unable to look into it now.The readtemp is on the 18M2, would it make a difference to swap them?
 

Jarubell

Senior Member
Thanks WA55, I did find that document late last night, and noted the differences but was still a little unsure.

The newer PICAXE parts poll the DS18B20 to allow continuation as fast as possible.
Is the 18M2+ one of these newer parts?
 

westaust55

Moderator
Thanks WA55, I did find that document late last night, and noted the differences but was still a little unsure.



Is the 18M2+ one of these newer parts?
Correct, 18M2 and 18M2+ are both 'newer' parts though the 18M2+ is the slightly newer of the two.

To Quote Technical from Rev Ed:
M2 and X2 parts do READTEMP conversions quicker than the M parts, as they continuously poll the device to 'ask it' when it has finished the conversion. The M parts simply wait 750ms regardless.
 

lbenson

Senior Member
> M2 and X2 parts do READTEMP conversions quicker than the M parts, as they continuously poll the device to 'ask it' when it has finished the conversion.

This leads to the suggestion of new commands, "prime_readtemp pin" and "prime readtemp12 pin", which would run in the background and would start the conversion, save the result and start a new conversion. Then any readtemp command would just return the latest result. readtemp without the "prime" command would perform as at present.

This would allow existing programs to run unbroken, but would allow faster operation for programs using the new command (at a cost, since behind the scenes there would be constant checking).
 

Jarubell

Senior Member
I was looking into parallel but I don't think that would help. Does anyone use parallel in their projects? For my current project, this issue is not a big deal if I readtemp every 30 seconds or so.

I like your suggestion.
 

BeanieBots

Moderator
lbenson;294275...This leads to the suggestion of new commands said:
I asked for this a few years ago and then we were blessed with the one-wire commands which do exactly that and more.
Maybe for the devices that do not support one-wire commands it might still be a possibility. No background polling, just start the conversion and leave it up to the programmer not to attempt a read until the correct conversion time has elapsed.

Does anyone use parallel in their projects?
All the time. Nearly all of my projects are some form of servo loop with DS18B20 sensors being read, serial comms back to a PC and servo loop iterations of typically 1Hz. This is only possible with X2 devices and the use of one-wire commands. Non time critical stuff such as sending data back to the PC is only done "when there is time" between each servo iteration. Creating multitasking applications can be quite daunting at first but once you get the hang of it, it's not very complicated. The thing to remember is that the PICAXE is not ultra fast and can only do one thing at a time, so understand the limitations before you start. With background serial, it is even possible to update the servo parameters in real time without stopping the process!
 

hippy

Technical Support
Staff member
Another option is multi-processing, adding a PICAXE-08M2 as a front-end . Untested but something like ...

Code:
  Master              Slave
.--------.         .---------.
|    RDY |<--------| TX      |     
|    REQ |-------->| RX   DS |<--->|) DS18B20
`--------'         `---------'
Code:
Symbol RX = C.3 : Symbol RX_PIN = pinC.3
Symbol TX = C.2
Symbol DS = C.1

Slave:
  Do
    Low TX
    ReadTemp12 DS, w0
    High TX
    Do : Loop While RX_PIN = 0
    High TX
    SerOut TX, T2400, ( b1, b0 ) 
  Loop
Code:
Symbol RDY = C.3 : Symbol RDY_PIN = pinC.3
Symbol REQ = C.2

Master:
  Do
    If RDY_PIN = 1 Then
      High REQ
      SerIn RDY, T2400, b1, b0
      Low REQ
      Gosub HandleTemperatureInW0
    End If
    Gosub DoOtherThings 
  Loop
With a bit of fiddling it should be possible to just require a single I/O pin on the Master.
 
Top