Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: LSM9DS1_Library_cal Motordriver
main.cpp
- Committer:
- apcastelein
- Date:
- 2016-12-02
- Revision:
- 4:104acd180bb1
- Parent:
- 3:b65b394375b7
- Child:
- 5:507618748cb4
File content as of revision 4:104acd180bb1:
#include "mbed.h" #include "LSM9DS1.h" #define PI 3.14159 //IMU used for monitoring orientation // - wired using i2c in cookbook example here // https://developer.mbed.org/components/LSM9DS1-IMU/ LSM9DS1 IMU(p9, p10, 0xD6, 0x3C); //Color sensor, used to assist robot positioning // - wired using i2c according to // https://developer.mbed.org/users/raj1995/notebook/adafruit-tcs34725-rgb-color-sensor/ I2C color_sensor(p28, p27); //pins for I2C communication (SDA, SCL) int color_addr = 41 << 1; int color_thresh = 10000;//threshold for green before it triggers bool on_color = false; //pc serial connection, for debugging purposes Serial pc(USBTX, USBRX); //initialize and calibrate IMU void initIMU(){ IMU.begin(); if (!IMU.begin()) { pc.printf("Failed to communicate with LSM9DS1.\n"); } IMU.calibrate(1); IMU.calibrateMag(0); } //verify color sensor and initialize void initColorSensor(){ pc.baud(9600); // Connect to the Color sensor and verify color_sensor.frequency(200000); char id_regval[1] = {146}; char data[1] = {0}; color_sensor.write(color_addr,id_regval,1, true); color_sensor.read(color_addr,data,1,false); // Initialize color sensor char timing_register[2] = {129,0}; color_sensor.write(color_addr,timing_register,2,false); char control_register[2] = {143,0}; color_sensor.write(color_addr,control_register,2,false); char enable_register[2] = {128,3}; color_sensor.write(color_addr,enable_register,2,false); } //initialization for all components void init(){ initIMU(); initColorSensor(); } // Sends a request to the server and waits for a path response char* getPathFromServer(){ //use wifi module here //https://developer.mbed.org/users/electromotivated/notebook/wifi-pid-redbot-robot-webserver/ char* path = ""; return path; } //gets the orientation from imu float getDirection(){ float sum = 0; int target_samples = 100; int num_samples = 0; while(num_samples++ < target_samples){ while(!IMU.magAvailable(X_AXIS)); IMU.readMag(); float my = IMU.calcMag(IMU.my); float mx = IMU.calcMag(IMU.mx); float dir; if (my == 0.0) dir = (mx < 0.0) ? 180.0 : 0.0; else dir = atan2(mx, my)*360.0/(2.0*PI); sum += dir; } return sum / target_samples; } //returns true when color is entered (subsequently returns false until color is exited and entered again) bool colorEntered(){ char green_reg[1] = {152}; char green_data[2] = {0,0}; color_sensor.write(color_addr,green_reg,1, true); color_sensor.read(color_addr,green_data,2, false); int green_value = ((int)green_data[1] << 8) | green_data[0]; bool is_green = green_value > color_thresh; bool color_triggered = !on_color && is_green; on_color = is_green; return color_triggered; } //technologies for navigation may include // - Line following https://www.sparkfun.com/products/11769 // - color sensor (indicates when end of movement is reached) void moveForward(){} void rotateLeft(){} void rotateRight(){} //arem technology is yet to be determined, will probably involve adjusting a motor void armUp(){} void armDown(){} // Loops through path commands and executes each one sequentially void executePath(char* path){ int index = 0; while(path[index] != '\0'){ switch(path[index]){ case 'F': moveForward(); break; case 'L': rotateLeft(); break; case 'R': rotateRight(); break; case 'U': armUp(); break; case 'D': armDown(); break; } } } //main code //- repeatedly listens for paths and then executes them int main() { init(); while(true){ char* path = getPathFromServer(); executePath(path); } }