LacqueyFetch firmware for the mbed

Dependencies:   Motor mbed

Committer:
raulsr988
Date:
Wed Apr 04 14:55:48 2018 +0000
Revision:
2:8ebb11ba21be
Parent:
1:3b3e8f8352a8
Child:
3:f31b6ddb9cd3
firmware adapted to the orders of the old code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
raulsr988 0:f8a72cc60181 1 /**
raulsr988 0:f8a72cc60181 2 *
raulsr988 0:f8a72cc60181 3 * @ingroup teo_body_lacqueyFetch
raulsr988 0:f8a72cc60181 4 * \defgroup teo_body_MbedFirmware Mbed-Firmware
raulsr988 0:f8a72cc60181 5 *
raulsr988 0:f8a72cc60181 6 * @brief Firmware that allows to recieve CAN messages to open and close the hand
raulsr988 0:f8a72cc60181 7 *
raulsr988 0:f8a72cc60181 8 * @section teo_body_firmware_legal Legal
raulsr988 0:f8a72cc60181 9 *
raulsr988 0:f8a72cc60181 10 * Copyright: 2018 (C) Universidad Carlos III de Madrid
raulsr988 0:f8a72cc60181 11 *
raulsr988 0:f8a72cc60181 12 * Author: <a href="http://roboticslab.uc3m.es/roboticslab/people/r-de-santos">Raul de Santos Rico</a>
raulsr988 0:f8a72cc60181 13 *
raulsr988 0:f8a72cc60181 14 * CopyPolicy: Released under the terms of the LGPLv2.1 or later, see license/LGPL.TXT
raulsr988 0:f8a72cc60181 15 *
raulsr988 0:f8a72cc60181 16 * @section lacqueyFetch_install Installation
raulsr988 0:f8a72cc60181 17 *
raulsr988 1:3b3e8f8352a8 18 * First you need to program the firmware to the MBED <br>
raulsr988 0:f8a72cc60181 19 * You can use the online compiler (https://os.mbed.com/compiler) to compile the firmware and stored it on the flash mbed memory<br>
raulsr988 0:f8a72cc60181 20 * Steps:
raulsr988 0:f8a72cc60181 21 * - Change the "id" of the device corresponding to the hand used (right-hand: ID 65, left-hand: ID 64) <br>
raulsr988 1:3b3e8f8352a8 22 * - Press "Compile" and save the .bin file on the flash mbed memory <br>
raulsr988 0:f8a72cc60181 23 * - If you want to check the correct functionality, you can connect the mbed to the serial USB and with a serial monitor program (configured with a baudrate of 115200), see the CAN messages information of the hand. <br>
raulsr988 0:f8a72cc60181 24 *
raulsr988 0:f8a72cc60181 25 *
raulsr988 1:3b3e8f8352a8 26 * @section lacqueyFetch_running Running (assuming correct installation)
raulsr988 0:f8a72cc60181 27 *
raulsr988 1:3b3e8f8352a8 28 * Running "launchManipulation" application, you can open or close the hand with this parameters (assuming you know how to run "launchManipulation"): <br>
raulsr988 2:8ebb11ba21be 29 * set pos 6 1200 -> open hand <br>
raulsr988 2:8ebb11ba21be 30 * set pos 6 -1200 -> close hand <br>
raulsr988 2:8ebb11ba21be 31 * set pos 6 0 -> loose hand <br>
raulsr988 0:f8a72cc60181 32 *
raulsr988 0:f8a72cc60181 33 **/
raulsr988 0:f8a72cc60181 34
raulsr988 0:f8a72cc60181 35 #include "mbed.h"
raulsr988 0:f8a72cc60181 36 #include "Motor.h"
raulsr988 0:f8a72cc60181 37
raulsr988 0:f8a72cc60181 38 Serial pc(USBTX, USBRX); // Serial connection
raulsr988 0:f8a72cc60181 39 DigitalOut led1(LED1); // received
raulsr988 0:f8a72cc60181 40 CAN can(p30, p29); // tx,rx
raulsr988 0:f8a72cc60181 41 Motor m(p24, p26, p25); // pwm, fwd, rev (PWM, REVERSE, FORDWARD)
raulsr988 0:f8a72cc60181 42
raulsr988 0:f8a72cc60181 43
raulsr988 2:8ebb11ba21be 44 bool receive(char* order) {
raulsr988 0:f8a72cc60181 45 int8_t id = 65; // right-hand: ID 65, left-hand: ID 64
raulsr988 2:8ebb11ba21be 46 char data;
raulsr988 0:f8a72cc60181 47 CANMessage msg;
raulsr988 0:f8a72cc60181 48 if( can.read(msg) ) {
raulsr988 0:f8a72cc60181 49 if(id == (int8_t)msg.id){
raulsr988 0:f8a72cc60181 50 pc.printf("Message received (ID: %d) (size: %d)\n", (int8_t)msg.id, msg.len);
raulsr988 2:8ebb11ba21be 51 data = msg.data[0] & 0b11110000;
raulsr988 0:f8a72cc60181 52 *order = data;
raulsr988 0:f8a72cc60181 53 led1 = !led1;
raulsr988 0:f8a72cc60181 54 return true;
raulsr988 0:f8a72cc60181 55 }
raulsr988 0:f8a72cc60181 56 }
raulsr988 0:f8a72cc60181 57
raulsr988 0:f8a72cc60181 58 return false;
raulsr988 0:f8a72cc60181 59 }
raulsr988 0:f8a72cc60181 60
raulsr988 0:f8a72cc60181 61 int main() {
raulsr988 0:f8a72cc60181 62 // set up the serial usb connection
raulsr988 0:f8a72cc60181 63 pc.baud(115200); //-- configure PC baudrate
raulsr988 0:f8a72cc60181 64 pc.printf("main()\n");
raulsr988 0:f8a72cc60181 65 // set up the can bus
raulsr988 0:f8a72cc60181 66 can.frequency(1000000); //-- 1Mbit/s (the same as the drivers configuration)
raulsr988 0:f8a72cc60181 67 can.reset();
raulsr988 0:f8a72cc60181 68
raulsr988 0:f8a72cc60181 69 // message received
raulsr988 2:8ebb11ba21be 70 char data;
raulsr988 0:f8a72cc60181 71
raulsr988 0:f8a72cc60181 72 while(1) {
raulsr988 0:f8a72cc60181 73
raulsr988 0:f8a72cc60181 74 if(receive(&data)) {
raulsr988 2:8ebb11ba21be 75 pc.printf("value received: %x\n", data);
raulsr988 2:8ebb11ba21be 76 switch(data){
raulsr988 2:8ebb11ba21be 77 case 0xA0: // open hand
raulsr988 2:8ebb11ba21be 78 m.speed(1.0);
raulsr988 2:8ebb11ba21be 79 break;
raulsr988 2:8ebb11ba21be 80
raulsr988 2:8ebb11ba21be 81 case 0xC0: // close hand
raulsr988 2:8ebb11ba21be 82 m.speed(-1.0);
raulsr988 2:8ebb11ba21be 83 break;
raulsr988 2:8ebb11ba21be 84
raulsr988 2:8ebb11ba21be 85 case 0xF0: // loose hand
raulsr988 2:8ebb11ba21be 86 m.speed(0.0);
raulsr988 2:8ebb11ba21be 87 break;
raulsr988 2:8ebb11ba21be 88
raulsr988 2:8ebb11ba21be 89 default: // loose hand
raulsr988 2:8ebb11ba21be 90 m.speed(0.0);
raulsr988 2:8ebb11ba21be 91 break;
raulsr988 2:8ebb11ba21be 92 }
raulsr988 0:f8a72cc60181 93 }
raulsr988 0:f8a72cc60181 94 }
raulsr988 0:f8a72cc60181 95 }