Practical Robotics Modular Robot Library
robot.h@3:8762f6b2ea8d, 2016-11-28 (annotated)
- Committer:
- jah128
- Date:
- Mon Nov 28 22:41:14 2016 +0000
- Revision:
- 3:8762f6b2ea8d
- Parent:
- 2:bf34b86aa0f3
- Child:
- 4:c2e933d53bea
Added first RPi input
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jah128 | 0:8a2dd255c508 | 1 | |
jah128 | 0:8a2dd255c508 | 2 | |
jah128 | 0:8a2dd255c508 | 3 | #ifndef ROBOT_H |
jah128 | 0:8a2dd255c508 | 4 | #define ROBOT_H |
jah128 | 0:8a2dd255c508 | 5 | |
jah128 | 0:8a2dd255c508 | 6 | #include "mbed.h" |
jah128 | 0:8a2dd255c508 | 7 | #include "led.h" |
jah128 | 0:8a2dd255c508 | 8 | #include "sensors.h" |
jah128 | 0:8a2dd255c508 | 9 | #include "motors.h" |
jah128 | 0:8a2dd255c508 | 10 | #include "calibration.h" |
jah128 | 0:8a2dd255c508 | 11 | |
jah128 | 0:8a2dd255c508 | 12 | #define LED_ADDRESS 0xC0 |
jah128 | 0:8a2dd255c508 | 13 | #define ADC_ADDRESS 0x90 |
jah128 | 0:8a2dd255c508 | 14 | |
jah128 | 1:a6728adaf7e7 | 15 | // SERIAL INTERFACES SETTINGS |
jah128 | 1:a6728adaf7e7 | 16 | |
jah128 | 1:a6728adaf7e7 | 17 | /* ENABLE_BLUETOOTH [1=on, 0=off]: Enable if the BlueSmirf module is being used */ |
jah128 | 1:a6728adaf7e7 | 18 | /** @brief Enable if the BlueSmirf module is being used. 0=off 1=on*/ |
jah128 | 1:a6728adaf7e7 | 19 | #define ENABLE_BLUETOOTH 1 |
jah128 | 1:a6728adaf7e7 | 20 | |
jah128 | 1:a6728adaf7e7 | 21 | /* ENABLE_PC_SERIAL [1=on, 0=off]: Enable if the PC(RPi) USB serial module is being used */ |
jah128 | 1:a6728adaf7e7 | 22 | #define ENABLE_PC_SERIAL 1 |
jah128 | 1:a6728adaf7e7 | 23 | |
jah128 | 1:a6728adaf7e7 | 24 | /* BLUETOOTH_BAUD [recommended=115200]: Baud rate for the BlueSMIRF module */ |
jah128 | 1:a6728adaf7e7 | 25 | #define BLUETOOTH_BAUD 115200 |
jah128 | 1:a6728adaf7e7 | 26 | |
jah128 | 1:a6728adaf7e7 | 27 | /* PC_BAUD [recommended=460800 for optimal performance, 115200 for compatability]: Baud rate for the PC USB serial module */ |
jah128 | 1:a6728adaf7e7 | 28 | //#define PC_BAUD 460800 |
jah128 | 1:a6728adaf7e7 | 29 | #define PC_BAUD 115200 |
jah128 | 1:a6728adaf7e7 | 30 | |
jah128 | 1:a6728adaf7e7 | 31 | /* DEBUG_MODE [1=on, 0=off]: Enable to allow debug messages to be sent of one of the serial interfaces */ |
jah128 | 1:a6728adaf7e7 | 32 | #define DEBUG_MODE 1 |
jah128 | 1:a6728adaf7e7 | 33 | |
jah128 | 1:a6728adaf7e7 | 34 | /* DEBUG_OUTPUT_STREAM [1=PC\USB 2=BlueSmirf 4=Display]: Specify which output stream(s) should be used by default for debug messages, if enabled*/ |
jah128 | 1:a6728adaf7e7 | 35 | #define DEBUG_OUTPUT_STREAM 1 |
jah128 | 1:a6728adaf7e7 | 36 | |
jah128 | 0:8a2dd255c508 | 37 | // To update sensors 10 times a second (8 x 0.0125 = 0.1) |
jah128 | 0:8a2dd255c508 | 38 | #define SENSOR_TICKER_PERIOD 0.0125 |
jah128 | 0:8a2dd255c508 | 39 | |
jah128 | 0:8a2dd255c508 | 40 | // H-Bridge should work at upto 100kHz (10uS) but note it seems to behave unusually at frequencies close to but above this |
jah128 | 0:8a2dd255c508 | 41 | // Slower speeds work a bit faster but noisier |
jah128 | 0:8a2dd255c508 | 42 | #define MOTOR_PWM_PERIOD_US 400 |
jah128 | 0:8a2dd255c508 | 43 | |
jah128 | 0:8a2dd255c508 | 44 | #define USE_STALL_OFFSET 1 |
jah128 | 0:8a2dd255c508 | 45 | #define STALL_OFFSET 0.22 |
jah128 | 0:8a2dd255c508 | 46 | |
jah128 | 0:8a2dd255c508 | 47 | extern I2C primary_i2c; |
jah128 | 3:8762f6b2ea8d | 48 | extern DigitalIn rpi1; |
jah128 | 2:bf34b86aa0f3 | 49 | extern DigitalOut case_led; |
jah128 | 2:bf34b86aa0f3 | 50 | extern DigitalOut mbed_led1; |
jah128 | 2:bf34b86aa0f3 | 51 | extern DigitalOut mbed_led2; |
jah128 | 2:bf34b86aa0f3 | 52 | extern DigitalOut mbed_led3; |
jah128 | 2:bf34b86aa0f3 | 53 | extern DigitalOut mbed_led4; |
jah128 | 0:8a2dd255c508 | 54 | extern AnalogIn vin_battery; |
jah128 | 0:8a2dd255c508 | 55 | extern Serial pc; |
jah128 | 0:8a2dd255c508 | 56 | extern Led led; |
jah128 | 0:8a2dd255c508 | 57 | extern Sensors sensors; |
jah128 | 0:8a2dd255c508 | 58 | extern Motors motors; |
jah128 | 0:8a2dd255c508 | 59 | extern volatile char i2c_lock; |
jah128 | 3:8762f6b2ea8d | 60 | extern char status_message [16]; |
jah128 | 0:8a2dd255c508 | 61 | |
jah128 | 1:a6728adaf7e7 | 62 | /** |
jah128 | 1:a6728adaf7e7 | 63 | * The Robot class contains the core functions for the robot |
jah128 | 1:a6728adaf7e7 | 64 | */ |
jah128 | 0:8a2dd255c508 | 65 | class Robot |
jah128 | 0:8a2dd255c508 | 66 | { |
jah128 | 0:8a2dd255c508 | 67 | public: |
jah128 | 0:8a2dd255c508 | 68 | |
jah128 | 0:8a2dd255c508 | 69 | /** |
jah128 | 0:8a2dd255c508 | 70 | * Main initialisation routine: setup the robot, the I2C interfaces, start system timers etc. |
jah128 | 0:8a2dd255c508 | 71 | * |
jah128 | 0:8a2dd255c508 | 72 | */ |
jah128 | 0:8a2dd255c508 | 73 | void init(void); |
jah128 | 0:8a2dd255c508 | 74 | |
jah128 | 0:8a2dd255c508 | 75 | /** |
jah128 | 0:8a2dd255c508 | 76 | * Get the uptime for the MBED |
jah128 | 0:8a2dd255c508 | 77 | * |
jah128 | 0:8a2dd255c508 | 78 | * @return The amount of time in seconds that the MBED has been active since last reset |
jah128 | 0:8a2dd255c508 | 79 | */ |
jah128 | 0:8a2dd255c508 | 80 | float get_uptime(void); |
jah128 | 0:8a2dd255c508 | 81 | |
jah128 | 0:8a2dd255c508 | 82 | /** |
jah128 | 0:8a2dd255c508 | 83 | * Get the battery voltage |
jah128 | 0:8a2dd255c508 | 84 | * |
jah128 | 0:8a2dd255c508 | 85 | * The battery voltage is passed through a 7.5V Zener diode, then into a 1:1 potential divider. This |
jah128 | 0:8a2dd255c508 | 86 | * allows a voltage in the approximate range 7.5 to 13.8V to be measured. |
jah128 | 0:8a2dd255c508 | 87 | * |
jah128 | 0:8a2dd255c508 | 88 | * @return The current voltage reading for the battery |
jah128 | 0:8a2dd255c508 | 89 | */ |
jah128 | 0:8a2dd255c508 | 90 | float get_battery_voltage(void); |
jah128 | 0:8a2dd255c508 | 91 | |
jah128 | 1:a6728adaf7e7 | 92 | /** |
jah128 | 1:a6728adaf7e7 | 93 | * Display a string message (printf) on the selected debug output stream [eg pc, bt or both] |
jah128 | 1:a6728adaf7e7 | 94 | */ |
jah128 | 1:a6728adaf7e7 | 95 | void debug(const char* format, ...); |
jah128 | 1:a6728adaf7e7 | 96 | |
jah128 | 1:a6728adaf7e7 | 97 | /** |
jah128 | 1:a6728adaf7e7 | 98 | * Setup the serial interfaces (pc, bt) at the correct baud rate and attach listeners |
jah128 | 1:a6728adaf7e7 | 99 | */ |
jah128 | 1:a6728adaf7e7 | 100 | void setup_serial_interfaces(void); |
jah128 | 1:a6728adaf7e7 | 101 | |
jah128 | 3:8762f6b2ea8d | 102 | /** |
jah128 | 3:8762f6b2ea8d | 103 | * Updates the 14-byte status message |
jah128 | 3:8762f6b2ea8d | 104 | */ |
jah128 | 3:8762f6b2ea8d | 105 | void update_status_message(void); |
jah128 | 3:8762f6b2ea8d | 106 | |
jah128 | 0:8a2dd255c508 | 107 | private: |
jah128 | 0:8a2dd255c508 | 108 | void _update_minutes(void); |
jah128 | 1:a6728adaf7e7 | 109 | void _bt_rx_callback(void); |
jah128 | 1:a6728adaf7e7 | 110 | void _pc_rx_callback(void); |
jah128 | 0:8a2dd255c508 | 111 | }; |
jah128 | 0:8a2dd255c508 | 112 | |
jah128 | 0:8a2dd255c508 | 113 | |
jah128 | 0:8a2dd255c508 | 114 | #endif |