TrekkingPhoenix / Mbed 2 deprecated TrekkingControllerV1-4_WinterChallenge20

Dependencies:   mbed mbed-rtos MotionSensor EthernetInterface

Files at this revision

API Documentation at this revision

Comitter:
drelliak
Date:
Sun May 15 19:13:53 2016 +0000
Parent:
17:34fa73413afb
Child:
19:c709c5a9fb08
Commit message:
Updated protocol

Changed in this revision

EthernetInterface.lib Show annotated file Show diff for this revision Revisions of this file
Protocol/protocol.h Show diff for this revision Revisions of this file
Protocol/receiver.cpp Show diff for this revision Revisions of this file
Protocol/receiver.h Show diff for this revision Revisions of this file
SensorsLibrary/FXOS8700.lib Show annotated file Show diff for this revision Revisions of this file
SensorsLibrary/FXOS8700CQ.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/EthernetInterface.lib	Mon May 02 00:58:20 2016 +0000
+++ b/EthernetInterface.lib	Sun May 15 19:13:53 2016 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/EthernetInterface/#f81b90d9a441
+https://developer.mbed.org/teams/TrekkingPhoenix/code/EthernetInterface/#403af2883ca6
--- a/Protocol/protocol.h	Mon May 02 00:58:20 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,112 +0,0 @@
-/**
-@file protocol.h
-@brief Protocol definitions.
-*/
-
-/*
-Copyright 2016 Erik Perillo <erik.perillo@gmail.com>
-
-This file is part of piranha-ptc.
-
-This is free software: you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation, either version 3 of the License, or
-(at your option) any later version.
-
-This is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-See the GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this. If not, see <http://www.gnu.org/licenses/>.
-*/
-
-
-#ifndef __PIRANHA_PROTOCOL_H__
-#define __PIRANHA_PROTOCOL_H__
-
-//@{
-///PID parameters range.
-#define PID_PARAMS_MIN -100.0
-#define PID_PARAMS_MAX 100.0
-//@}
-
-//@{
-///Ground velocity range.
-#define GND_VEL_MIN -100.0
-#define GND_VEL_MAX 100.0
-//@}
-
-//@{
-///Angle reference range (in radians).
-#define PI 3.141593
-#define ANG_REF_MIN -PI
-#define ANG_REF_MAX PI
-//@}
-
-//@{
-///Jogging speed period (in seconds).
-#define JOG_VEL_PERIOD_MIN 0.0
-#define JOG_VEL_PERIOD_MAX 300.0
-//@}
-
-//@{
-///Jogging speed ratio.
-#define JOG_VEL_RATIO_MIN 0.0
-#define JOG_VEL_RATIO_MAX 1.0
-//@}
-
-//@{
-///Jogging speed period (in seconds).
-#define MAG_CALIB_MIN -1000.0
-#define MAG_CALIB_MAX 1000.0
-//@}
-
-///Messages to send via protocol.
-enum
-{
-	///Do nothing.
-	NONE,
-
-	///Brake the robot.
-	BRAKE,
-
-	///Reset angle measurement.
-	ANG_RST,
-
-	///Set new angle reference.
-	ANG_REF,
-
-	///Set new ground velocity for robot.
-	GND_VEL,
-
-	///Set new jogging speed for robot.
-	JOG_VEL,
-
-	///Magnetometer calibration.
-	MAG_CALIB,
-
-	///Send PID control parameters.
-	PID_PARAMS
-};
-
-#define MSG_HEADER_SIZE 1
-#define MSG_VAL_SIZE 2
-#define MSG_MAX_NUM_VALS 4
-#define MSG_BUF_LEN (MSG_HEADER_SIZE + MSG_VAL_SIZE*MSG_MAX_NUM_VALS)
-#define MSG_HEADER_IDX 0
-#define MSG_VALS_START_IDX (MSG_HEADER_IDX + 1)
-
-#define SENDER_PORT 7532
-#define SENDER_IFACE_ADDR "192.168.7.1"
-#define SENDER_NETMASK_ADDR "255.255.255.0"
-#define SENDER_GATEWAY_ADDR "0.0.0.0"
-
-#define RECEIVER_PORT 7533
-#define RECEIVER_IFACE_ADDR "192.168.7.2"
-#define RECEIVER_NETMASK_ADDR "255.255.255.0"
-#define RECEIVER_GATEWAY_ADDR "0.0.0.0"
-
-#endif
-
--- a/Protocol/receiver.cpp	Mon May 02 00:58:20 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,143 +0,0 @@
-/*
-Copyright 2016 Erik Perillo <erik.perillo@gmail.com>
-
-This file is part of piranha-ptc.
-
-This is free software: you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation, either version 3 of the License, or
-(at your option) any later version.
-
-This is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-See the GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this. If not, see <http://www.gnu.org/licenses/>.
-*/
-
-
-#include "receiver.h"
-#include "EthernetInterface.h"
-
-float Receiver::un_scale(uint16_t value, float min, float max)
-{
-	return ((float)value)/((1 << 16) - 1)*(max - min) + min;
-}
-
-uint8_t Receiver::get_header()
-{
-	return this->message[MSG_HEADER_IDX];
-}
-
-uint16_t Receiver::get_raw_val(int pos)
-{
-	uint16_t value = 0;
-
-	value |= this->message[MSG_VALS_START_IDX + 2*pos];
-	value |= this->message[MSG_VALS_START_IDX + 2*pos + 1] << 8;
-
-	return value;
-}
-
-float Receiver::get_val(float min, float max, int pos)
-{
-	uint16_t raw_val;
-
-	raw_val = this->get_raw_val(pos);
-	return this->un_scale(raw_val, min, max);
-}
-
-void Receiver::get_vals(float min, float max, float* vals, int size)
-{
-	uint16_t raw_val;
-
-	for(int i=0; i<size; i++)
-	{
-		raw_val = this->get_raw_val(i);
-		vals[i] = this->un_scale(raw_val, min, max);
-	}
-}
-
-bool Receiver::receive()
-{
-	return this->sock.receiveFrom(this->sender_addr, this->message, 
-		sizeof(this->message)) > 0;
-}
-
-Receiver::Receiver()
-{
-	;
-}
-
-Receiver::Receiver(Endpoint sender_addr, const UDPSocket& sock):
-	sock(sock), sender_addr(sender_addr)
-{
-	;
-}
-
-Receiver::Receiver(Endpoint sender_addr, int sock_port, int timeout):
-	sender_addr(sender_addr)
-{
-	this->sock.bind(sock_port);
-	this->sock.set_blocking(timeout < 0, timeout);
-}
-
-void Receiver::set_sender_addr(const Endpoint& sender_addr)
-{
-	this->sender_addr = sender_addr;
-}
-
-void Receiver::set_socket(const UDPSocket& sock)
-{
-	this->sock = sock;
-}
-
-void Receiver::set_socket(int port, int timeout)
-{
-	this->sock.bind(port);
-	this->sock.set_blocking(timeout < 0, timeout);
-}
-
-Endpoint Receiver::get_sender_addr()
-{
-	return this->sender_addr;
-}
-
-UDPSocket Receiver::get_socket()
-{
-	return this->sock;
-}
-
-uint8_t Receiver::get_msg()
-{
-	return this->message[MSG_HEADER_IDX];
-}
-
-float Receiver::get_ang_ref()
-{
-	return this->get_val(ANG_REF_MIN, ANG_REF_MAX);
-}
-
-float Receiver::get_gnd_vel()
-{
-	return this->get_val(GND_VEL_MIN, GND_VEL_MAX);
-}
-
-void Receiver::get_jog_vel(float* period, float* ratio)
-{
-	*period = this->get_val(JOG_VEL_PERIOD_MIN, JOG_VEL_PERIOD_MAX);
-	*ratio = this->get_val(JOG_VEL_RATIO_MIN, JOG_VEL_RATIO_MAX, 1);
-}
-
-void Receiver::get_pid_params(float* params)
-{
-	this->get_vals(PID_PARAMS_MIN, PID_PARAMS_MAX, params, 4);
-}
-
-void Receiver::get_mag_calib(float* params)
-{
-	this->get_vals(MAG_CALIB_MIN, MAG_CALIB_MAX, params, 4);
-}
-
--- a/Protocol/receiver.h	Mon May 02 00:58:20 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,74 +0,0 @@
-/**
-@file receiver.h
-@brief Receiver side functions declarations.
-*/
-
-/*
-Copyright 2016 Erik Perillo <erik.perillo@gmail.com>
-
-This file is part of piranha-ptc.
-
-This is free software: you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation, either version 3 of the License, or
-(at your option) any later version.
-
-This is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-See the GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this. If not, see <http://www.gnu.org/licenses/>.
-*/
-
-
-#ifndef __PIRANHA_RCV_PROTOCOL_H__
-#define __PIRANHA_RCV_PROTOCOL_H__
-
-#include "protocol.h"
-#include "mbed.h"
-#include "EthernetInterface.h"
-
-#define TEST
-
-class Receiver
-{
-        
-	#ifdef TEST
-	public:
-	#else
-	protected:
-	#endif
-	UDPSocket sock;
-	char message[MSG_BUF_LEN];
-	Endpoint sender_addr;	
-
-	float un_scale(uint16_t value, float min, float max);
-	uint8_t get_header();
-	uint16_t get_raw_val(int pos=0);
-	float get_val(float min, float max, int pos=0);
-	void get_vals(float min, float max, float* vals, int size);
-
-	public:
-	Receiver();
-	Receiver(Endpoint sender_addr, const UDPSocket& sock);
-	Receiver(Endpoint sender_addr, int sock_port=RECEIVER_PORT, int timeout=1);
-
-	void set_sender_addr(const Endpoint& sender_addr);
-	void set_socket(const UDPSocket& sock);
-	void set_socket(int port=RECEIVER_PORT, int timeout=1);
-	Endpoint get_sender_addr();
-	UDPSocket get_socket();
-
-	bool receive();
-	uint8_t get_msg();
-	float get_ang_ref();
-	float get_gnd_vel();
-	void get_jog_vel(float* period, float* ratio);
-	void get_pid_params(float* params);
-	void get_mag_calib(float* params);
-};
-
-#endif
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SensorsLibrary/FXOS8700.lib	Sun May 15 19:13:53 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/AswinSivakumar/code/FXOS8700/#98ea52282575
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SensorsLibrary/FXOS8700CQ.lib	Sun May 15 19:13:53 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/trm/code/FXOS8700CQ/#e2fe752b881e
--- a/main.cpp	Mon May 02 00:58:20 2016 +0000
+++ b/main.cpp	Sun May 15 19:13:53 2016 +0000
@@ -99,7 +99,7 @@
 Ticker controller_ticker;
 
 // Magnetometer variables and functions
-float max_x, max_y, min_x, min_y,x,y;
+float max_x=0, max_y=0, min_x=0, min_y=0,x,y;
 MotionSensorDataUnits mag_data;
 MotionSensorDataCounts mag_raw;
 float processMagAngle();
@@ -118,18 +118,18 @@
     motor.setVelocity(0);
     
     // Protocol parameters
+    printf("Initializing Ethernet!\r\n");
     set_leds_color(RED, red_led, green_led, blue_led);
     eth.init(RECEIVER_IFACE_ADDR, RECEIVER_NETMASK_ADDR, RECEIVER_GATEWAY_ADDR);
     eth.connect();
+    printf("Protocol initialized! \r\n");
     set_leds_color(BLUE, red_led, green_led, blue_led);
     rcv.set_socket();
-    
     gyro.start_measure(GYRO_PERIOD);
     controller_ticker.attach(&control,Ts);
     //main loop
     while(1){
         readProtocol();
-        wait(0.01);
     }
 }
 void control(){
@@ -139,75 +139,64 @@
     if(!rcv.receive())
         return;
     char msg = rcv.get_msg();
+    printf("Message received!");
     switch(msg)
     {
         case NONE:
-            //ser.printf("sending red signal to led\r\n");
-             red_led = LED_ON;
-
+            //ser.printf("sending red signal to led\r\n");  
+            set_leds_color(BLACK,red_led,green_led,blue_led);          
             printf("NONE\r\n");
-
             wait(1);
-            red_led = LED_OFF;
             break;          
         case BRAKE:
             //ser.printf("sending green signal to led\r\n");
-            green_led = LED_ON;
+            set_leds_color(YELLOW,red_led,green_led,blue_led);
             motor.stopJogging();
             printf("BRAKE\r\n");
             motor.brakeMotor();
-            green_led = LED_OFF;
             break;
         case ANG_RST:
             //ser.printf("sending blue signal to led\r\n");
-            blue_led = LED_ON;
-
+            set_leds_color(PURPLE,red_led,green_led,blue_led);
             printf("ANG_RST\r\n");
             gyro.stop_measure();
+            printf("Stopped gyro\r\n");
             gyro.start_measure(GYRO_PERIOD);
-            blue_led = LED_OFF;
+            printf("Starting Gyro\r\n");
             initializeController();
             break;
         case ANG_REF:
-            red_led = LED_ON;
-            green_led = LED_ON;
-
-            reference = rcv.get_ang_ref();// - processMagAngle();
+            set_leds_color(GREEN,red_led,green_led,blue_led);
+            reference = rcv.get_ang_ref() - processMagAngle();
             printf("New reference: %f \n\r",reference*180/PI);
             if(reference > PI)
                 reference = reference - 2*PI;
-            if(reference < -PI)
+            else if(reference < -PI)
                 reference = reference + 2*PI;  
-            red_led = LED_OFF;
-            green_led = LED_OFF;
+            break;
+        case CAM_ANG_REF:
+            set_leds_color(RED,red_led,green_led,blue_led);
+            reference = rcv.get_cam_ang_ref();
+            printf("New reference: %f \n\r",reference*180/PI); 
             break;
         case GND_VEL:
-            red_led = LED_ON;
-            blue_led = LED_ON;
+            set_leds_color(AQUA,red_led,green_led,blue_led);
             float vel = rcv.get_gnd_vel();
             motor.setVelocity(vel);      
             printf("GND_VEL = %f\r\n", vel);
-
-            red_led = LED_OFF;
-            blue_led = LED_OFF;
             break;
         case JOG_VEL:
-            red_led = LED_ON;
-            blue_led = LED_ON;
-
+            set_leds_color(WHITE,red_led,green_led,blue_led);
             float p, r;
             rcv.get_jog_vel(&p,&r);
+            printf("Joggin with period %f and duty cycle %f\r\n",p,r);
             if(p == 0 || r == 0)
                 motor.stopJogging();
             else
                 motor.startJogging(r,p);
-            red_led = LED_OFF;
-            blue_led = LED_OFF;
             break;
         case PID_PARAMS:
-            blue_led = LED_ON;
-            green_led = LED_ON;
-
+            set_leds_color(RED,red_led,green_led,blue_led);
             float ar[4];
             rcv.get_pid_params(ar);
             P = ar[0];
@@ -218,26 +207,20 @@
                 ar[0], ar[1], ar[2], ar[3]);
 
             wait(1);
-            blue_led = LED_OFF;
-            green_led = LED_OFF;
             break;
         case MAG_CALIB:
+            set_leds_color(BLUE,red_led,green_led,blue_led);
+            printf("MAG_CALIB\r\n");
             float mag[4];
             rcv.get_mag_calib(mag);
             max_x=mag[1];
             max_y=mag[3];
             min_x=mag[0];
             min_y=mag[2];
+            printf(" max_x = %f, max_y= %f, min_x= %f, min_y= %f\r\n",mag[1],mag[3],mag[0],mag[2]);
             break;
         default:
-            blue_led = LED_ON;
-            green_led = LED_ON;
-            red_led = LED_ON;
             printf("nothing understood\r\n");
-            wait(1);
-            blue_led = LED_OFF;
-            green_led = LED_OFF;
-            red_led = LED_OFF;
             //ser.printf("unknown command!\r\n");
     }
 }
@@ -312,8 +295,23 @@
 
 /* Function to transform the magnetometer reading in angle(rad/s).*/
 float processMagAngle(){
-    mag.getAxis(mag_data);
-    x = 2*(mag_data.x-min_x)/float(max_x-min_x) - 1;
-    y = 2*(mag_data.y-min_y)/float(max_y-min_y) - 1;
-    return -atan2(y,x);
+    printf("starting ProcessMagAngle()\n\r");
+    float mag_lpf = 0;
+    Timer t1;
+    for(int i = 0; i<20; i++){
+        //printf("\r\n");
+        //wait(0.1);
+        t1.start();
+        __disable_irq();
+        mag.getAxis(mag_data);
+        __enable_irq();
+        t1.stop();
+        x = 2*(mag_data.x-min_x)/float(max_x-min_x) - 1;
+        y = 2*(mag_data.y-min_y)/float(max_y-min_y) - 1;
+        mag_lpf = mag_lpf + (-atan2(y,x));
+        wait(0.015);
+        }
+   // wait(20*0.01);
+    printf("Finished ProcessMagAngle() %d \n\r",t1.read_us());
+    return mag_lpf/20;
 }
\ No newline at end of file