Mario Bambagini / Mbed 2 deprecated rover_car

Dependencies:   mbed-rtos mbed ssWi

Files at this revision

API Documentation at this revision

Comitter:
mariob
Date:
Tue Mar 12 07:35:28 2013 +0000
Commit message:
rover project - car side; MB

Changed in this revision

carDrivers/Servo/servo.cpp Show annotated file Show diff for this revision Revisions of this file
carDrivers/Servo/servo.hpp Show annotated file Show diff for this revision Revisions of this file
carDrivers/car.cpp Show annotated file Show diff for this revision Revisions of this file
carDrivers/car.hpp 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
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
ssWi.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/carDrivers/Servo/servo.cpp	Tue Mar 12 07:35:28 2013 +0000
@@ -0,0 +1,27 @@
+#include "servo.hpp"
+
+
+Servo::Servo (PinName pwm, float min, float max, float center): _pwm(pwm)
+{
+    _pwm.period(0.02);
+    _min = min;
+    _max = max;
+    _center = center;
+    *this = 0.5;
+}
+
+Servo &Servo::operator= (float angle)
+{
+    if (angle>1.0)
+        angle = 1.0;
+    else
+        if (angle<-1.0)
+            angle = -1.0;
+    _pwm.pulsewidth(_center+((_max-_min)*(angle/2.0)));
+    return *this;
+}
+
+Servo::operator float()
+{
+    return _pwm.read();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/carDrivers/Servo/servo.hpp	Tue Mar 12 07:35:28 2013 +0000
@@ -0,0 +1,24 @@
+#ifndef __SERVO_HPP__
+#define __SERVO_HPP__
+
+
+#include "mbed.h"
+
+
+class Servo
+{
+    PwmOut _pwm;
+    float _min;
+    float _max;
+    float _center;
+    
+public:
+    Servo(PinName pwm, float min=0.001, float max=0.002, float center=0.0015);
+
+    Servo& operator= (float speed);
+    
+    operator float();
+
+};
+
+#endif //__SERVO_HPP__
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/carDrivers/car.cpp	Tue Mar 12 07:35:28 2013 +0000
@@ -0,0 +1,30 @@
+#include "car.hpp"
+
+Car::Car (  PinName pinCamX, PinName pinCamY,
+            PinName pinSteering, PinName pinMotor):
+                        cameraX(pinCamX), cameraY(pinCamY),
+                        steering(pinSteering), motor(pinMotor)
+{
+    angleX = 0;
+    angleY = 0;
+    cameraX = 0.0;
+    cameraY = 0.0;
+    steering = 0.0;
+    motor = 0.0;
+}
+
+void Car::setSpeed (float s)
+{
+    motor = s;
+}
+
+void Car::setSteering (float angle)
+{
+    steering = -angle;
+}
+
+void Car::setCamera (float x, float y)
+{
+    cameraX = x;
+    cameraY = y;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/carDrivers/car.hpp	Tue Mar 12 07:35:28 2013 +0000
@@ -0,0 +1,34 @@
+#ifndef __CAR_HPP__
+#define __CAR_HPP__
+
+
+#include "servo.hpp"
+
+
+#define CAMERA_ANGLE_STEP 0.1
+
+
+class Car
+{
+    //camera
+    int angleX;
+    int angleY;
+    Servo cameraX;
+    Servo cameraY;
+
+    //motor
+    Servo steering;
+    Servo motor;
+
+public:
+
+    Car (PinName pinCamX, PinName pinCamY, PinName pinSteering, PinName pinMotor);
+
+    void setSpeed (float s);
+    void setSteering (float angle);
+    void setCamera (float x, float y);
+
+};
+
+
+#endif //__CAR_HPP__
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Mar 12 07:35:28 2013 +0000
@@ -0,0 +1,67 @@
+#include "mbed.h"
+#include "rtos.h"
+
+#include "xbee.hpp"
+#include "ssWiSocket.hpp"
+#include "car.hpp"
+
+
+#define PORT_MOTOR 20
+#define PORT_SERVO 30
+#define PORT_SERVO_CAMERA_X 40
+#define PORT_SERVO_CAMERA_Y 50
+
+#define UPDATING_FUNCTION_MS 200
+
+
+ssWiSocket* socket_motor;
+ssWiSocket* socket_servo;
+ssWiSocket* socket_camera_x;
+ssWiSocket* socket_camera_y;
+
+Car car(p23, p24, p25, p26);
+
+
+void udateFunction (const void* arg);
+
+
+int main()
+{
+    //Communication link connection
+    XBeeModule xbee(p9, p10, 102, 14);
+    xbee.setDstAddress(XBeeBroadcastAddress());
+    xbee.init(-1, 5);
+
+    //Socket creation
+    socket_motor = ssWiSocket::createSocket(PORT_MOTOR);
+    socket_servo = ssWiSocket::createSocket(PORT_SERVO);
+    socket_camera_x = ssWiSocket::createSocket(PORT_SERVO_CAMERA_X);
+    socket_camera_y = ssWiSocket::createSocket(PORT_SERVO_CAMERA_Y);
+    if (socket_motor==NULL || socket_servo==NULL ||
+                    socket_camera_x==NULL || socket_camera_y==NULL)
+        printf("Error: sockets\n\r");
+
+    //Timer setting
+    RtosTimer timer(udateFunction, osTimerPeriodic, NULL);
+    timer.start(200);
+
+    //Infinite wait
+    Thread::wait(osWaitForever);
+}
+
+
+void udateFunction (const void* arg)
+{
+    static DigitalOut led(LED1);
+    static int var = 0;
+//    while(1) {
+    car.setSpeed((float)(socket_motor->read())/1000.0);
+    car.setSteering((float)(socket_servo->read())/1000.0);
+    float x = (float)socket_camera_x->read()/1000.0;
+    float y = (float)socket_camera_y->read()/1000.0;
+    car.setCamera(x, y);
+    led = var>=10?1:0;
+    var = var>=20?0:var+1;
+//        Thread::wait(UPDATING_FUNCTION_MS);
+//    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Tue Mar 12 07:35:28 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#9654a71f5a90
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Mar 12 07:35:28 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/cd19af002ccc
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ssWi.lib	Tue Mar 12 07:35:28 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mariob/code/ssWi/#b50c3b3b241c