Skip to content
Snippets Groups Projects
Commit 07f24b60 authored by Václav Jelínek's avatar Václav Jelínek
Browse files

Add WDT, boot.py file to disable WDT at soft reboot, modify startup display message

parent f10daa00
No related branches found
No related tags found
No related merge requests found
......@@ -111,10 +111,13 @@ def display_show_error(robot):
robot.display.centered_text('error!', 30, 1)
robot.display.show()
def display_show_startup(robot):
def display_show_startup(robot, reset):
robot.display.fill(0)
robot.display.centered_text('Turning', 20, 1)
robot.display.centered_text('on', 30, 1)
if reset:
robot.display.centered_text('Resetting', 20, 1)
else:
robot.display.centered_text('Turning', 20, 1)
robot.display.centered_text('on', 30, 1)
robot.display.show()
def display_show_program(robot):
......
from machine import Pin, I2C, Timer
from machine import Pin, I2C, Timer, WDT
from time import sleep, ticks_ms
from lib.cube import Buzzer, I2CGuard, SH1106_I2C, Esp, I2C_master
......@@ -6,7 +6,7 @@ from lib import nxt, ev3, OC
from lib.hw_defs.ports import SENSOR_PORT_PINS, MOTOR_PORT_PINS
from lib.hw_defs.pins import *
from lib.robot_consts import Sensor,Button, BAT_VOLTAGE_TURNOFF, BAT_MEASURE_PERIOD, I2C_FREQ, PCF_CHECK_PERIOD, FW_VERSION
from lib.robot_consts import Sensor,Button, BAT_VOLTAGE_TURNOFF, BAT_TURNOFF_PERIODS, BAT_MEASURE_PERIOD, I2C_FREQ, PCF_CHECK_PERIOD, FW_VERSION
import motors_ll
import brick_ll
......@@ -48,16 +48,37 @@ class Robot:
# Initialize periodic cube buttons state read on PCF
self.cube_startup = True
self.pcf_timer = Timer(-1)
self.pcf_timer = Timer(-1)
self.pcf_timer.init(mode=Timer.PERIODIC, period=PCF_CHECK_PERIOD,
callback=self.read_buttons)
# Timer is used to check if the batteries are charged enough, turn off cube if not
# Timer is used to check if the batteries are charged enough, turn off cube if not
# counter -1 to grant extra period at cube startup so the battery multiplier can be set
self.battery_low_counter = -1
self.battery_timer = Timer(-1)
self.battery_timer.init(mode=Timer.PERIODIC, period=BAT_MEASURE_PERIOD,
callback=self.check_battery_low)
self.wdt = WDT(timeout=4000)
print("Watch Dog Timer initialised with timeout 4 s.")
self.wdt_feed_timer = Timer(-1)
self.wdt_feed_timer.init(period=3500, mode=Timer.PERIODIC,
callback=self.wdt_feed)
# Function called with a timer to feed Watch Dog Timer to prevent rebooting
def wdt_feed(self, t):
self.wdt.feed()
# Turn off cube
def turn_off(self):
self.turn_off_pin.value(1)
sleep(0.096)
self.led.on()
self.turn_off_pin.value(0) # resetting pin for quick startup
sleep(1)
while True:
self.turn_off_pin.value(1)
# Read cube buttons state on timer interrupt ant turn off cube if power button pressed
def read_buttons(self, p):
self.buttons.read_value()
......@@ -70,13 +91,7 @@ class Robot:
self.display.centered_text('Turning off', 30, 1)
self.display.show()
sleep(0.3)
self.turn_off_pin.value(1)
sleep(0.096)
self.led.on()
self.turn_off_pin.value(0)
sleep(1)
while True:
self.turn_off_pin.value(1)
self.turn_off()
def fw_version(self):
return FW_VERSION
......@@ -154,16 +169,24 @@ class Robot:
# Check if the battery voltage is too low,
# if it is make buzzer sound, show info on the display and turn off the cube
def check_battery_low(self, p):
print(self.battery.voltage() , self.battery_low_counter)
if self.battery.voltage() < BAT_VOLTAGE_TURNOFF:
self.buzzer.set_freq_duty(1000, 0.05)
self.display.fill(0)
self.display.text('Battery low!', 15, 30, 1)
self.display.show()
sleep(1)
self.buzzer.off()
sleep(3)
self.deinit_all()
self.buttons.turn_off_cube()
self.battery_low_counter += 1
if self.battery_low_counter >= BAT_TURNOFF_PERIODS:
#self.buzzer.set_freq_duty(1000, 0.05)
self.display.fill(0)
self.display.text('Battery low!', 15, 30, 1)
self.display.show()
self.wdt_feed(None) # Feed WDT to prevent rebooting
sleep(1)
self.wdt_feed(None)
self.buzzer.off()
sleep(3)
self.wdt_feed(None)
self.deinit_all()
self.turn_off()
else:
self.battery_low_counter = 0
# Helper object for storing, initializing and deinitializing sensor objects
......
......@@ -76,6 +76,7 @@ class Color:
BAT_MEASURE_PERIOD = const(1000) # ms
BAT_TURNOFF_PERIODS = 2
BAT_VOLTAGE_MAX = 8.4
BAT_VOLTAGE_MIN = 6.2
BAT_VOLTAGE_TURNOFF = (BAT_VOLTAGE_MIN+0.1) # Volts
......
# Disable Watchdog timer after a soft boot
machine.mem32[0x40058000] = machine.mem32[0x40058000] & ~(1<<30)
\ No newline at end of file
import micropython
# Allocate exception buffer for interrupt handlers
micropython.alloc_emergency_exception_buf(100)
import machine
from lib.robot import Robot
from lib.menu_display import display_show_startup
# Initialize global robot object
robot = Robot()
display_show_startup(robot)
# machine.WDT_RESET = hard and soft reboot or WDT reboot
display_show_startup(robot, machine.WDT_RESET == machine.reset_cause())
import time
import os
import sys
import micropython
import gc
import machine
import menu_programs
......@@ -21,8 +25,7 @@ try:
except:
from lib.default_config import esp_default_name, battery_multiplier
# Allocate exception buffer for interrupt handlers
micropython.alloc_emergency_exception_buf(100)
robot.battery.set_multiplier(battery_multiplier)
def main():
# File and directory constants for browsing user programs
......@@ -62,7 +65,8 @@ def main():
f.write("Previous log too long, file deleted\n")
f.write("--------------------------------------------------\n")
f.write(f"ERROR program: {program_name}, time since reset: {int(time.ticks_diff(time.ticks_ms(),robot.start_time))} ms\n")
sys.print_exception(e, f)
if e:
sys.print_exception(e, f)
f.write("--------------------------------------------------\n")
# Analyze pressed buttons for menu movement
......@@ -145,8 +149,6 @@ def main():
bt_connecting = False
button_values = robot.buttons.pressed()
esp_reset_failed_counter = 0
display_show_startup(robot)
robot.battery.set_multiplier(battery_multiplier)
# Get a list of user programs
program_list = []
......@@ -174,6 +176,7 @@ def main():
# Loop that shows display, controls menu and starts user programs
while(True):
#print(micropython.mem_info())
# Connect to and reset ESP32 to BT mode if it is not running
if not robot.esp.running() and counter % 10 == 0:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment