Hi,
I've gotten into basics of ESP8266 and am building a very basic robot for my son to entertain him (and me) c rawl before you walk approach...
Consists of basic script using hcscr04 distance sensor, szdoit motor shield and robot platform. Problem is when i execute the script it seems to crash and I'm unsure why.. so here I am. The system does not use interrupts or anything advanced just checks distance to object in front of robot and if nothing close moves forward, if something is close then choose a random angle and rotate robot to avoid and continue moving forward. To be enhanced once basics operational.
Code as follows, it seems to sense distance once, goes forward, never comes back to main function and then resets. Any guidance appreciated... I must be doing something basic wrong but can't see it for the life of me
The problem seems to be in the DRIVE script I would say as it never returns from there but if I comment it out the main script loop is evident in the log file
Another question as well... how would I go about declaring exception handling for ALL exceptions on the 8266..
try:
except Exception as ex:
#exception properties? code etc...?
Could not find any documentation and inbuilt object analysis of exception object does not provide any clues....
Thanks
Main script
=======
import drive
import hcsr04
import utime
import uos
import log
def main():
logfile = "robot.log"
duration = 1
speed = 750
sensor = hcsr04.HCSR04(14, 12)
drv = drive.DRIVE()
cycles = 0
l = log.Log()
while True:
dist = sensor.distance_mm()
l.append(logfile, "distance: {}".format(dist))
if (dist == -1 or dist > 200):
l.append(logfile, "forward")
drv.forward(speed, duration)
else:
drv.reset()
newangle = int.from_bytes(uos.urandom(1), 'big')
newdir = int.from_bytes(uos.urandom(1), 'big')
l.append(logfile, "deviation: {} / Angle: {}".format(newdir, newangle))
if newdir > 180:
drv.left(speed, newangle)
else:
drv.right(speed, newangle)
cycles = cycles + 1
l.append(logfile, "cycles: {} | free mem: {}".format(cycles, gc.mem_free()))
gc.collect()
utime.sleep_ms(1000)
DRIVE script
========
import machine
#import utime
#mode 1 -> forward
#mode 2 -> reverse
#mode 3 -> left
#mode 4 -> right
class DRIVE(object):
def __init__(self):
self.mode = 0
self.pdirA = machine.Pin(0, machine.Pin.OUT, machine.Pin.PULL_UP) #D3 / GPIO0
self.pwmA = machine.PWM(machine.Pin(5), freq=1023)
self.pdirB = machine.Pin(2, machine.Pin.OUT, machine.Pin.PULL_UP) #D4 / GPIO2
self.pwmB = machine.PWM(machine.Pin(4), freq=1023)
def forward(self, duty=512,duration = 10):
#self.pdirA.value(0)
#self.pdirB.value(0)
self.mode = 1
self.pwmA.duty(duty)
self.pwmB.duty(duty)
#utime.sleep(duration)
#self.reset()
#self.settimer(duration)
return mode
def reverse(self, duty=512,duration = 10):
self.pdirA.value(1)
self.pdirB.value(1)
self.mode = 2
self.pwmA.duty(duty)
self.pwmB.duty(duty)
#utime.sleep(duration)
#self.reset()
#self.settimer(duration)
return mode
def left(self, duty=512,duration = 10):
self.pdirA.value(0)
self.pdirB.value(1)
self.mode = 3
self.pwmA.duty(duty)
self.pwmB.duty(duty)
#utime.sleep(duration)
#self.reset()
#self.settimer(duration)
return mode
def right(self, duty=512,duration = 10):
self.pdirA.value(1)
self.pdirB.value(0)
self.mode = 4
self.pwmA.duty(duty)
self.pwmB.duty(duty)
#utime.sleep(duration)
#self.reset()
#self.settimer(duration)
return mode
def settimer(self, duration):
self.tmrdur = machine.Timer(-1)
self.tmrdur.init(period=1000*duration, mode=machine.Timer.ONE_SHOT, callback=self.tmrcallback)
def tmrcallback(self, tmr):
self.pwmA.duty(0)
self.pwmB.duty(0)
self.mode = 0
def reset(self):
self.pwmA.duty(0)
self.pwmB.duty(0)
self.mode = 0
return mode
ESP8266 is resetting
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: ESP8266 is resetting
Your DRIVE class has a number of instances of
As far as I can see this variable doesn't exist (you have self.mode).
What I don't understand is why you haven't seen an error message. When you test the robot, have you got a means of viewing the output at the REPL? Either via a USB interface or the WebREPL?
Code: Select all
return mode
What I don't understand is why you haven't seen an error message. When you test the robot, have you got a means of viewing the output at the REPL? Either via a USB interface or the WebREPL?
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: ESP8266 is resetting
Yep there's been no error messages on repl, was running it via webrepl not via serial. Could I potentially be getting more error info via serial?
I added the mode afterwards but even before returning mode it was already resetting.
Introduced the logging to et a better idea of what might be happening but to date has not been very useful.
Anything obviously wrong with the implementation?
I added the mode afterwards but even before returning mode it was already resetting.
Introduced the logging to et a better idea of what might be happening but to date has not been very useful.
Anything obviously wrong with the implementation?