Remote controlled robot with LEDs and Wemos motor shield

Dependencies:   RF24 mbed

Files at this revision

API Documentation at this revision

Comitter:
Makodan
Date:
Wed Sep 27 16:03:48 2017 +0000
Commit message:
Remote controlled robot with LEDs and Wemos motor shield

Changed in this revision

RF24.lib Show annotated file Show diff for this revision Revisions of this file
config.h 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
main.h 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
wemos_mc.cpp Show annotated file Show diff for this revision Revisions of this file
wemos_mc.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 51c02b15eb70 RF24.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RF24.lib	Wed Sep 27 16:03:48 2017 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/akashvibhute/code/RF24/#ef74df512fed
diff -r 000000000000 -r 51c02b15eb70 config.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/config.h	Wed Sep 27 16:03:48 2017 +0000
@@ -0,0 +1,38 @@
+#ifndef CONFIG_H
+#define CONFIG_H
+
+
+
+    #define OWN_ID      1
+ 
+    #define RadioChannel 0x4C
+    #define DataAddress 0xF0F0F0F0D2 //0xc2c2c2c2c2
+
+    #define ledpin      PA_8
+    #define unused_pin  PA_1
+    
+    #define bump        PA_11
+    #define odom        PA_12
+    
+    #define red         PA_0
+    #define green       PA_3
+    #define blue        PA_2
+    
+    #define nrf_CE      PB_4
+    #define nrf_CSN     PB_3
+    #define spi_SCK     PA_5
+    #define spi_MOSI    PA_7
+    #define spi_MISO    PA_6
+    #define nrf_irq     PA_15 
+    
+    #define i2c_sda     PB_7 
+    #define i2c_scl     PB_6
+    
+    #define RXD         PA_3
+    #define TXD         PA_2
+    
+
+
+#endif
+
+
diff -r 000000000000 -r 51c02b15eb70 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Sep 27 16:03:48 2017 +0000
@@ -0,0 +1,195 @@
+#include "mbed.h"
+#include "config.h"
+#include "main.h"
+#include "RF24.h"
+#include "wemos_mc.h"
+
+/*
+* Constructors and pin settings
+*/
+DigitalOut myled(ledpin);
+RF24 radio(spi_MOSI, spi_MISO, spi_SCK, nrf_CE, nrf_CSN );
+
+I2C ms(i2c_sda, i2c_scl);
+
+InterruptIn receive(nrf_irq);
+InterruptIn bumper(bump);
+//InterruptIn odomet(odom);
+
+DigitalOut strip_red(red);
+DigitalOut strip_blue(green);
+DigitalOut strip_green(blue);
+
+/*
+* Global variables
+*/
+volatile char rec;
+volatile float _pwm = 0;
+volatile bool newmsg_flag=0;
+volatile bool stop_flag=0;
+volatile dataIN input;
+volatile uint32_t roll_counter = 0;
+
+/*
+* Private function prototypes
+*/
+void RadioSetup();
+void change_strip(uint16_t col);
+void startAnimation();
+
+/*
+* Interrupt callbacks
+*/
+void read_radio(){
+    dataIN tmp;     
+    myled = !myled;
+    radio.read(&tmp,sizeof(tmp));
+    if(tmp.ID == OWN_ID){
+        input.dir_L = tmp.dir_L;
+        input.pwm_L = tmp.pwm_L;
+        input.dir_R = tmp.dir_R;
+        input.pwm_R = tmp.pwm_R;
+        input.led = tmp.led;
+        newmsg_flag=1;
+        }     
+    }
+         
+void front_hit(){
+         //blinking_flag = 1;
+         //blinking_phase = 0;
+         
+         stop_flag=1;
+         }         
+
+void roll(){
+         roll_counter++;
+         
+         if(roll_counter % 2) change_strip(GREEN);
+         if(roll_counter % 3) change_strip(OFF);
+         
+         }
+         
+int main(){
+    ms.frequency(100000);
+    setfreq(ms, 1000);
+    
+    setmotor(ms, _MOTOR_L, _STOP, 0);
+    setmotor(ms, _MOTOR_R, _STOP, 0);
+    change_strip(OFF);
+   
+    RadioSetup();
+    radio.startListening();
+    
+    startAnimation();
+
+    receive.fall(&read_radio);
+    bumper.rise(&front_hit);
+ //   odomet.rise(&roll);
+    
+
+
+    
+     while(1){
+        
+                if(newmsg_flag == 1){       
+                     setmotor(ms, _MOTOR_L, (uint8_t)input.dir_L, input.pwm_L);
+                     wait_ms(10);
+                     setmotor(ms, _MOTOR_R, (uint8_t)input.dir_R, input.pwm_R);  
+                     change_strip(input.led);
+                     newmsg_flag = 0;
+                    }        
+               
+                 if(stop_flag == 1){
+                     setmotor(ms, _MOTOR_L, _STOP, 0);
+                     setmotor(ms, _MOTOR_R, _STOP, 0);
+                     change_strip(RED);
+                     wait(2);
+                     change_strip(OFF);
+                     stop_flag = 0;
+                   }
+        
+        /*
+               if(blinking_phase<5 && blinking_flag == 1){
+                   change_strip(current_col);
+                   wait_ms(250);
+                   change_strip(OFF);
+                   wait_ms(250);
+                    blinking_phase++;
+                   }      
+          */     
+                         
+        }
+        
+    
+    }
+    
+void RadioSetup(){
+    
+    radio.begin();
+    radio.setPALevel(RF24_PA_HIGH);
+    radio.setChannel(RadioChannel);
+    radio.setRetries(0,2);
+    radio.enableDynamicAck();
+    radio.enableDynamicPayloads();
+    radio.openReadingPipe(1,DataAddress);
+    radio.maskIRQ(1,1,0);
+    }
+    
+void change_strip(uint16_t col){
+      
+    switch(col){
+        case RED:
+             strip_red = 1;
+             strip_blue = 0;
+             strip_green=0;
+            break;
+        case GREEN:
+             strip_red = 0;
+             strip_blue = 0;
+             strip_green = 1;
+            break;
+        case BLUE:
+             strip_red = 0;
+             strip_blue = 1;
+             strip_green = 0;
+            break;
+        case YELLOW:
+             strip_red = 1;
+             strip_blue = 0;
+             strip_green = 1;
+            break;
+        case MAGENTA:
+             strip_red = 1;
+             strip_blue = 1;
+             strip_green = 0;
+            break;
+        case CYAN:
+             strip_red = 0;
+             strip_blue = 1;
+             strip_green = 1;
+            break;
+        case WHITE:
+             strip_red = 1;
+             strip_blue = 1;
+             strip_green = 1;
+            break;
+        case OFF:
+             strip_red = 0;
+             strip_blue = 0;
+             strip_green = 0;
+            break;    
+        }
+    
+    }
+    
+    void startAnimation(){
+        
+        change_strip(RED);
+        wait_ms(500);
+        change_strip(GREEN);
+        wait_ms(500);
+        change_strip(BLUE);
+        wait_ms(500);
+        change_strip(OFF);
+        
+        }
\ No newline at end of file
diff -r 000000000000 -r 51c02b15eb70 main.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.h	Wed Sep 27 16:03:48 2017 +0000
@@ -0,0 +1,22 @@
+enum{
+    OFF = 0,
+    RED = 1,
+    GREEN = 2,
+    BLUE = 3,
+    YELLOW = 4,
+    MAGENTA = 5,
+    CYAN = 6,
+    WHITE = 7
+    };
+    
+typedef struct{
+        
+    uint16_t ID;
+    uint16_t dir_L;
+    float pwm_L;
+    uint16_t dir_R;
+    float pwm_R;
+    uint16_t led;
+    
+    }dataIN;
+    
diff -r 000000000000 -r 51c02b15eb70 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Sep 27 16:03:48 2017 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/675da3299148
\ No newline at end of file
diff -r 000000000000 -r 51c02b15eb70 wemos_mc.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wemos_mc.cpp	Wed Sep 27 16:03:48 2017 +0000
@@ -0,0 +1,61 @@
+#include "wemos_mc.h"
+
+
+/* setfreq() -- set PWM's frequency
+freq:
+    PWM's frequency 
+*/
+void setfreq(I2C& i2c, uint32_t freq){
+        
+        char data[4];
+        
+        data[0]=(((char)(freq >> 16)) & (char)0x0f);
+        data[1]=((char)(freq >> 16));
+        data[2]=((char)(freq >> 8));
+        data[3]=((char)freq);
+        
+        //__disable_irq(); 
+        i2c.write( _ADDRESS, data, 4 );
+        //__enable_irq(); 
+        wait_ms(5);
+
+}
+
+/* setmotor() -- set motor
+motor:
+    _MOTOR_A    0   Motor A
+    _MOTOR_B    1   Motor B
+dir:
+    _SHORT_BRAKE    0
+    _CW            1
+    _CCW            2
+    _STOP           3
+    _STANDBY        4
+pwm_val:
+    0.00 - 100.00  (%)
+*/
+void setmotor(I2C& i2c,uint8_t motor, uint8_t dir, float pwm_val){
+        
+        uint16_t _pwm_val = (uint16_t)pwm_val * 100;
+        
+        if(_pwm_val>10000)
+            _pwm_val=10000;
+        
+        char data[4];
+         
+        data[0] = ( motor | (char)0x10);
+        data[1] = ( dir );
+        data[2] = ((char)(_pwm_val >> 8));
+        data[3] = ((char)_pwm_val);
+        
+        //__disable_irq(); 
+        i2c.write( _ADDRESS, data, 4 );
+        //__enable_irq(); 
+        wait_ms(5);
+        
+        }
+
+void setmotor(I2C& i2c, uint8_t motor, uint8_t dir){
+        
+        setmotor(i2c,motor,dir,100);
+}
\ No newline at end of file
diff -r 000000000000 -r 51c02b15eb70 wemos_mc.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wemos_mc.h	Wed Sep 27 16:03:48 2017 +0000
@@ -0,0 +1,26 @@
+#ifndef __WEMOS_MC_H
+#define __WEMOS_MC_H
+
+#include "mbed.h"
+#define _MOTOR_L 0
+#define _MOTOR_R 1
+
+#define _ADDRESS 0x60
+
+#define _SHORT_BRAKE 0
+#define _CCW  1
+#define _CW     2
+#define _STOP 3
+#define _STANDBY 4
+
+
+
+    void setfreq(I2C& i2c, uint32_t freq);
+    void setmotor(I2C& i2c, uint8_t motor, uint8_t dir, float pwm_val);
+    void setmotor(I2C& i2c, uint8_t motor, uint8_t dir);
+
+   
+
+
+
+#endif
\ No newline at end of file