Program for testing the ES20X board (version 2.0/2.1) I/O servo ports, motor driver ports, analog inputs, RS232 serial, digital port p5,p6,p7,p8,p11 as digital outputs

Dependencies:   MotCon ServoOut mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // J. Bradshaw 20160111
00002 // Program for testing I/O on mbed ES201 Dev Board V1.1
00003 #include "mbed.h"
00004 #include "ServoOut.h"   //uses ServoOut.h library for generating servo pulses
00005 #include "MotCon.h"     //uses the MotCon.h library for controlling the motor ports
00006 #include "rtos.h"
00007 
00008 #define PI 3.1415926
00009 
00010 char DB9_M_char;
00011 
00012 DigitalOut led1(LED1);
00013 DigitalOut led3(LED3);
00014 
00015 //PC serial connection
00016 Serial pc(USBTX, USBRX);    //tx, rx via USB connection
00017 I2C i2c(p9,p10);    //SDA, SCL
00018 Serial DB9_male(p13, p14); //DB9 serial male
00019 
00020 //Port J5
00021 BusOut bus_J5(p5, p6, p7, p8, p11);
00022 
00023 //header for J5
00024 DigitalOut J5_p5(p5);
00025 DigitalOut J5_p6(p6);
00026 DigitalOut J5_p7(p7);
00027 DigitalOut J5_p8(p8);
00028 DigitalOut J5_p11(p11);
00029 
00030 //Analog port in J6
00031 AnalogIn    J6_P16(p16);
00032 AnalogIn    J6_P17(p17);
00033 AnalogIn    J6_P18(p18);
00034 AnalogIn    J6_P19(p19);
00035 AnalogIn    J6_P20(p20);
00036 
00037 // servo ports p21 - p24
00038 ServoOut sv1(p21);
00039 ServoOut sv2(p22);
00040 ServoOut sv3(p23);
00041 ServoOut sv4(p24);
00042 
00043 /*
00044 DigitalOut mot1_ph1(p28);
00045 DigitalOut mot1_ph2(p27);
00046 PwmOut mot_en1(p25);
00047 
00048 DigitalOut mot2_ph1(p29);
00049 DigitalOut mot2_ph2(p30);
00050 PwmOut mot_en2(p26);
00051 */
00052 
00053 MotCon m1(p25, p27, p28);
00054 MotCon m2(p26, p29, p30);
00055 
00056 void mot1_thread(void const *args){
00057         while(1){
00058         // Run PWM and servo command signals
00059         for(float cycle=0;cycle<2.0*PI;cycle+=.078){
00060             sv1 = 1500 + (int)(sin(cycle)*500);
00061             sv2 = 1500 + (int)(cos(cycle)*500);
00062             sv3 = 1500 + (int)(sin(cycle)*500);
00063             sv4 = 1500 + (int)(sin(cycle)*500);
00064         
00065             m1.mot_control(.85*sin(cycle));
00066             m2.mot_control(.85*cos(cycle));
00067                         
00068             pc.printf("cycle = %.2f  \r", cycle);
00069             Thread::wait(10);
00070         } 
00071     }//while(1)
00072 }
00073 
00074 void digOut_thread(void const *args){
00075     //toggle bus I/O for testing
00076     while(1){
00077         bus_J5 = 0x1f;
00078         Thread::wait(500);
00079         bus_J5 = 0xAA;
00080         Thread::wait(100);
00081         bus_J5 = 0x55;
00082         Thread::wait(100);
00083         bus_J5 = 0xAA;
00084         Thread::wait(100);
00085         bus_J5 = 0x55;
00086         Thread::wait(100);        
00087         bus_J5 = 0; //all off
00088         Thread::wait(500);    
00089     }
00090 }
00091 
00092 void led1_flash(void const *args){
00093     while(1){
00094         led1 = !led1;
00095         Thread::wait(500);
00096     }
00097 }
00098 
00099 void serial_DB9M_test(void const *args){
00100     while(1){        
00101         for(char tx_char='a';tx_char <= 'z';tx_char++){
00102             DB9_male.printf("%c", tx_char);
00103             
00104             if(DB9_male.readable()){
00105                 DB9_M_char = DB9_male.getc();
00106             }            
00107             Thread::wait(100);
00108         }
00109     }        
00110 }
00111 
00112 void i2c_test(void const *args){
00113     while(1){        
00114         //i2c.write(0x55);
00115         Thread::wait(10);
00116     }        
00117 }
00118 //------------ Main ------------------------------
00119 int main() {
00120     float analogVal[6];
00121     
00122     i2c.frequency(100000);
00123     pc.baud(9600);
00124     DB9_male.baud(9600);
00125     
00126     Thread t1(mot1_thread);
00127     Thread t2(digOut_thread);
00128     Thread t3(led1_flash);
00129     Thread t4(serial_DB9M_test);
00130     Thread t5(i2c_test);
00131         
00132     while(1) {        
00133         analogVal[0] = J6_P16;
00134         analogVal[1] = J6_P17;
00135         analogVal[2] = J6_P18;
00136         analogVal[3] = J6_P19;
00137         analogVal[4] = J6_P20;
00138         pc.printf("%.2f %.2f %.2f %.2f %.2f DB9M=%c\r\n", analogVal[0],analogVal[1],analogVal[2],analogVal[3],analogVal[4], DB9_M_char);            
00139         wait(.05);
00140     }
00141 }