Practical Robotics Modular Robot Library
robot.cpp@0:8a2dd255c508, 2016-11-26 (annotated)
- Committer:
- jah128
- Date:
- Sat Nov 26 17:28:53 2016 +0000
- Revision:
- 0:8a2dd255c508
- Child:
- 1:a6728adaf7e7
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jah128 | 0:8a2dd255c508 | 1 | #include "robot.h" |
jah128 | 0:8a2dd255c508 | 2 | |
jah128 | 0:8a2dd255c508 | 3 | Timer mbed_uptime; |
jah128 | 0:8a2dd255c508 | 4 | int timer_minute_count; |
jah128 | 0:8a2dd255c508 | 5 | Ticker timer_ticker; |
jah128 | 0:8a2dd255c508 | 6 | volatile char i2c_lock = 0; |
jah128 | 0:8a2dd255c508 | 7 | |
jah128 | 0:8a2dd255c508 | 8 | Led led; |
jah128 | 0:8a2dd255c508 | 9 | Sensors sensors; |
jah128 | 0:8a2dd255c508 | 10 | Motors motors; |
jah128 | 0:8a2dd255c508 | 11 | I2C primary_i2c (p9, p10); |
jah128 | 0:8a2dd255c508 | 12 | AnalogIn vin_battery(p17); |
jah128 | 0:8a2dd255c508 | 13 | Serial pc(USBTX,USBRX); |
jah128 | 0:8a2dd255c508 | 14 | |
jah128 | 0:8a2dd255c508 | 15 | |
jah128 | 0:8a2dd255c508 | 16 | // Public functions |
jah128 | 0:8a2dd255c508 | 17 | |
jah128 | 0:8a2dd255c508 | 18 | void Robot::init(){ |
jah128 | 0:8a2dd255c508 | 19 | |
jah128 | 0:8a2dd255c508 | 20 | //Start the uptime timer |
jah128 | 0:8a2dd255c508 | 21 | timer_minute_count = 0; |
jah128 | 0:8a2dd255c508 | 22 | timer_ticker.attach(this,&Robot::_update_minutes, 300); //Update minutes resets the uptime timer every 5 minutes to prevent stop overruns |
jah128 | 0:8a2dd255c508 | 23 | mbed_uptime.start(); |
jah128 | 0:8a2dd255c508 | 24 | |
jah128 | 0:8a2dd255c508 | 25 | //Setup the primary i2c bus to 400kHz baud rate |
jah128 | 0:8a2dd255c508 | 26 | primary_i2c.frequency(400000); |
jah128 | 0:8a2dd255c508 | 27 | |
jah128 | 0:8a2dd255c508 | 28 | //Send a reset signal to the LED driver |
jah128 | 0:8a2dd255c508 | 29 | pc.printf("Resetting LED driver : "); |
jah128 | 0:8a2dd255c508 | 30 | if(led.reset_led_driver() != 0)pc.printf("FAILED\n"); |
jah128 | 0:8a2dd255c508 | 31 | else pc.printf("SUCCESS\n"); |
jah128 | 0:8a2dd255c508 | 32 | wait(0.05); |
jah128 | 0:8a2dd255c508 | 33 | |
jah128 | 0:8a2dd255c508 | 34 | //Send the initialisation signal to the LED driver |
jah128 | 0:8a2dd255c508 | 35 | pc.printf("Initialising LED driver : "); |
jah128 | 0:8a2dd255c508 | 36 | if(led.init_led_driver() != 0)pc.printf("FAILED\n"); |
jah128 | 0:8a2dd255c508 | 37 | else pc.printf("SUCCESS\n"); |
jah128 | 0:8a2dd255c508 | 38 | |
jah128 | 0:8a2dd255c508 | 39 | wait(0.05); |
jah128 | 0:8a2dd255c508 | 40 | |
jah128 | 0:8a2dd255c508 | 41 | //Start the Sharp sensor ticker |
jah128 | 0:8a2dd255c508 | 42 | pc.printf("Starting distance sensor ticker...\n"); |
jah128 | 0:8a2dd255c508 | 43 | sensors.start_sensor_ticker(); |
jah128 | 0:8a2dd255c508 | 44 | |
jah128 | 0:8a2dd255c508 | 45 | wait(0.05); |
jah128 | 0:8a2dd255c508 | 46 | |
jah128 | 0:8a2dd255c508 | 47 | //Setup the h-bridge drivers |
jah128 | 0:8a2dd255c508 | 48 | pc.printf("Starting the H-bridge drivers...\n"); |
jah128 | 0:8a2dd255c508 | 49 | motors.init(); |
jah128 | 0:8a2dd255c508 | 50 | } |
jah128 | 0:8a2dd255c508 | 51 | |
jah128 | 0:8a2dd255c508 | 52 | float Robot::get_uptime(void) |
jah128 | 0:8a2dd255c508 | 53 | { |
jah128 | 0:8a2dd255c508 | 54 | return mbed_uptime.read() + (timer_minute_count * 60); |
jah128 | 0:8a2dd255c508 | 55 | } |
jah128 | 0:8a2dd255c508 | 56 | |
jah128 | 0:8a2dd255c508 | 57 | float Robot::get_battery_voltage(void) |
jah128 | 0:8a2dd255c508 | 58 | { |
jah128 | 0:8a2dd255c508 | 59 | // Voltage is measured through a 7.5V zener diode followed by a 1:1 potential divider |
jah128 | 0:8a2dd255c508 | 60 | |
jah128 | 0:8a2dd255c508 | 61 | float measure_voltage = ZENER_VOLTAGE + (vin_battery.read() * BATTERY_PD_MULTIPLIER); |
jah128 | 0:8a2dd255c508 | 62 | return measure_voltage; |
jah128 | 0:8a2dd255c508 | 63 | } |
jah128 | 0:8a2dd255c508 | 64 | |
jah128 | 0:8a2dd255c508 | 65 | // Private functions |
jah128 | 0:8a2dd255c508 | 66 | |
jah128 | 0:8a2dd255c508 | 67 | void Robot::_update_minutes() |
jah128 | 0:8a2dd255c508 | 68 | { |
jah128 | 0:8a2dd255c508 | 69 | mbed_uptime.reset(); |
jah128 | 0:8a2dd255c508 | 70 | timer_minute_count += 5; |
jah128 | 0:8a2dd255c508 | 71 | } |