Practical Robotics Modular Robot Library
Diff: robot.cpp
- Revision:
- 0:8a2dd255c508
- Child:
- 1:a6728adaf7e7
diff -r 000000000000 -r 8a2dd255c508 robot.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/robot.cpp Sat Nov 26 17:28:53 2016 +0000 @@ -0,0 +1,71 @@ +#include "robot.h" + +Timer mbed_uptime; +int timer_minute_count; +Ticker timer_ticker; +volatile char i2c_lock = 0; + +Led led; +Sensors sensors; +Motors motors; +I2C primary_i2c (p9, p10); +AnalogIn vin_battery(p17); +Serial pc(USBTX,USBRX); + + +// Public functions + +void Robot::init(){ + + //Start the uptime timer + timer_minute_count = 0; + timer_ticker.attach(this,&Robot::_update_minutes, 300); //Update minutes resets the uptime timer every 5 minutes to prevent stop overruns + mbed_uptime.start(); + + //Setup the primary i2c bus to 400kHz baud rate + primary_i2c.frequency(400000); + + //Send a reset signal to the LED driver + pc.printf("Resetting LED driver : "); + if(led.reset_led_driver() != 0)pc.printf("FAILED\n"); + else pc.printf("SUCCESS\n"); + wait(0.05); + + //Send the initialisation signal to the LED driver + pc.printf("Initialising LED driver : "); + if(led.init_led_driver() != 0)pc.printf("FAILED\n"); + else pc.printf("SUCCESS\n"); + + wait(0.05); + + //Start the Sharp sensor ticker + pc.printf("Starting distance sensor ticker...\n"); + sensors.start_sensor_ticker(); + + wait(0.05); + + //Setup the h-bridge drivers + pc.printf("Starting the H-bridge drivers...\n"); + motors.init(); +} + +float Robot::get_uptime(void) +{ + return mbed_uptime.read() + (timer_minute_count * 60); +} + +float Robot::get_battery_voltage(void) +{ + // Voltage is measured through a 7.5V zener diode followed by a 1:1 potential divider + + float measure_voltage = ZENER_VOLTAGE + (vin_battery.read() * BATTERY_PD_MULTIPLIER); + return measure_voltage; +} + +// Private functions + +void Robot::_update_minutes() +{ + mbed_uptime.reset(); + timer_minute_count += 5; +} \ No newline at end of file