<< Previous Post..

Table of Contents

Next Post >>


With Continuation to my Previous Post.. where I have shared all GUI Related codes and executable application, In this post I am Sharing My Hardware Details and mcu Source codes in open source domain...

 

1. Hardware Schematics, Hardware Details and PCB Layout

 

1.1 Schematic of CC3200-MOD based Master mcu Module for Chest Strap:

cc3200_schematic.png

cc3200_module pcb.png  8.JPG

 

  DSC03260.JPG 9.jpg  14.jpg



1.2 Schematic of MSP430 Slave mcu module for Halmet Impact Detection :

 

 

MSP430 Slave Module.png

slave pcb.png1.JPG  2.JPG  3.jpg

4.jpg  5.jpg  6.jpg  7.jpg

 

10.jpg  12.jpg

2. Source code of mcu MSP430 and CC3200

2.1 CC3200-mod Master mcu Source code : Source compiled with energia-0101E0015

 

/*
  Sudden Impact Wearable Design Challenge
  Project : Real Time Player Monitoring System
  Sponsered By element14 , Analog Devices, Tektronix and Electrolube
  Special Thanks to Texas Instruments for providing CC3200MOD, MSP430, TLV3402 Samples free of cost
  You can Track Progress of this project at..
  http://www.element14.com/community/community/design-challenges/sudden-impact/blog/2014/12/31/realtimemonitor-real-time-player-monitoring-system-table-of-contents

  Source code for Master MCU (CC3200)
  Interfaces :
  1. Two ADXL375 interface via i2c
  2. Micro-SD card interface via SPI
  3. Slave MCU (MSP430G2553)/SUB GHz TRx interfaced via UART
  4. ADT7320 Temp Sensor Interfaced via bit bangging SPI
  5. AD8232 Based Heart Rate monitor interfaced on Digital Pin with interrupt
  6. DS1307 RTC Interfaced on I2C Bus


  Source compiled with energia-0101E0015
  Target MCU : TI CC3200 or CC3200MOD

  By: Ravi Butani
  Date:29th April,2015
  e-mail: ravi_butani@yahoo.com
  Source Code is Released Under Creative Commons Attribution-ShareAlike 3.0 Unported Licence

*/


#include <SPI.h>
#include <SD.h>
#include <WiFi.h>
#include <Wire.h>
#include "RTClib.h"


#define FIFO_SIZE 40 // Fifo Size for get maximum Acceleration
#define SEND_COUNT 9


// AD8232/TLV3402 Interfaces
#define HR_INT 2 //GPIO3 PIN48
#define LOFF1 6 //GPIO4 PIN49
#define LOFF2 24 //GPIO5 PIN50


// LED Interface
#define LED1_SD 11 //GPIO22 PIN11
#define LED2_NW 3 //GPIO13 PIN10
#define LED3_HR 4 //GPIO12 PIN9


// ADT7320 Bit-Bangging SPI Interface
#define sck 5      //GPIO6 PIN51
#define miso 8    //GPIO7 PIN52
#define mosi 27    //GPIO8 PIN53
#define ss 29      // GPIO9 PIN54


const char ACC1=0x53; // Device address of ADXL375_1
const char ACC2=0x53; //0x1D; // Device address of ADXL375_2
short int _buff_acc1[3], _buff_acc2[3]; // Hold raw accelerometer data
short int send_acc1,send_acc2,body_temp,heart_rate=76;
short int _acc1_fifo[FIFO_SIZE],_acc2_fifo[FIFO_SIZE];
volatile uint32_t pre_millis = 0;
uint8_t fifo_point = 0;
uint8_t send_point = 0;
WiFiUDP Udp;
RTC_DS1307 rtc;
File myFile;
char ssid[]="------------------------------------";
char password[]="*********************************";
char log_file[]="pid1/20141212/121212.txt";
char log_dir[]="pid1/20141212";
int player_id = 0;
unsigned int localPort = 2390;      // local port to listen on
IPAddress broadcast_ip(1,1,1,1);
char packetBuffer[255]; //buffer to hold incoming packet
char ReplyBuffer[5];    // a string to send back




void setup()
{
  pinMode(HR_INT, INPUT);
  pinMode(LED1_SD, OUTPUT);
  pinMode(LED2_NW, OUTPUT);
  pinMode(LED3_HR, OUTPUT);
  attachInterrupt(HR_INT, hr_isr, RISING);
  pinMode(LOFF1, INPUT);
  pinMode(LOFF2, INPUT);

  // Open serial communications and I2C Bus:
  Serial.begin(9600);
  Wire.begin();
  rtc.begin();
  digitalWrite(LED1_SD,HIGH);
  digitalWrite(LED2_NW,HIGH);
  digitalWrite(LED3_HR,HIGH);
  delay(2000);
  digitalWrite(LED1_SD,LOW);
  digitalWrite(LED2_NW,LOW);
  digitalWrite(LED3_HR,LOW);

  Serial.println("\nRTPMS Start");

  //SD Card Initialization
  Serial.print("Init SD card...");
  while (!SD.begin(18)) {
    Serial.println("init failed!");
    digitalWrite(LED1_SD,HIGH);
    delay(80);
    digitalWrite(LED1_SD,LOW);
    delay(500);
  }
  Serial.println("init done.");
  digitalWrite(LED1_SD,LOW);

  read_nw_ssid_pw(ssid,password,player_id);

  set_logfile_name();
  Serial.println(player_id);
  Serial.print("Trying to connect to Nw named: ");
  Serial.println(ssid);
  // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
  WiFi.begin(ssid, password);
  while ( WiFi.status() != WL_CONNECTED) {
    // print dots while we wait to connect
    Serial.print(".");
    digitalWrite(LED2_NW,HIGH);
    delay(30);
    digitalWrite(LED2_NW,LOW);
    delay(300);
  }
  Serial.println("\nConnected to the network");
  Serial.println("Waiting for an ip");

  while (WiFi.localIP() == INADDR_NONE) {
    // print dots while we wait for an ip addresss
    Serial.print(".");
    digitalWrite(LED2_NW,HIGH);
    delay(10);
    digitalWrite(LED2_NW,LOW);
    delay(100);
  }


  Serial.println("\nIP Address obtained");
  // you're connected now, so print out the status
  printWifiStatus();
  Serial.println("\nStarting connection to server...");
  init_acc(ACC1);
  init_acc(ACC2);
  init_ADT7320();
  Udp.begin(localPort);
  set_logfile_name();
  write_marker_sd();
}


void loop()
{
  //read_acc(ACC1,_buff_acc1);
  read_acc(ACC2,_buff_acc2);
  check_update_uart_data();

  write_rawdata_sd(_buff_acc1,_buff_acc2,body_temp,heart_rate);
  _acc1_fifo[fifo_point]=max_acc_xyz(_buff_acc1);
  _acc2_fifo[fifo_point]=max_acc_xyz(_buff_acc2);
  fifo_point = fifo_point+1;
  if(fifo_point == FIFO_SIZE)fifo_point = 0;

  send_acc1 = max_fifo(_acc1_fifo)/2;
  send_acc2 = (max_fifo(_acc2_fifo)/4) - 3;
  if(send_acc2 <= 0){send_acc2 = 0;}
  body_temp = read_temp()-2;

  send_point = send_point+1;
  if(send_point == SEND_COUNT)
  {
    digitalWrite(LED3_HR,HIGH);
    //if(digitalRead(LOFF2) == HIGH){heart_rate=0;}
    send_point=0;
    send_packet_udp(player_id,send_acc1,send_acc2,body_temp,heart_rate);
    digitalWrite(LED3_HR,LOW);
    //send all data to app via udp
  }
  check_wifi_status();
  delay(10);
}


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  // print your WiFi IP address:
  IPAddress ip = WiFi.localIP();
  broadcast_ip = ip;
  broadcast_ip[3] = 255;
  Serial.print("IP: ");
  Serial.println(ip);
  Serial.print("Broadcast IP: ");
  Serial.println(broadcast_ip);
}


void read_nw_ssid_pw(char _ssid[],char _password[],int _player_id)
{
  char i=0,loc=0;
  //open config.txt file for reading SSID PW and PlayerID:
  myFile = SD.open("nwconfig.txt");
  if (myFile)
  {
      while (myFile.available()&& loc==0)
      {
        _ssid[i] = myFile.read();
        if(_ssid[i]==' ' )
        {
          _ssid[i] = 0x00; loc=1;
        }
        i++;
      }
      i=0;
      while (myFile.available()&& loc==1)
      {
        _password[i] = myFile.read();
        if(_password[i]==' ' )
        {
        _password[i] = 0x00; loc=2;
        }
        i++;
      }
      i=0;
      while (myFile.available()&& loc==2)
      {
        _player_id = (myFile.read()-0x30)*10;
        _player_id = _player_id + myFile.read()-0x30;
        player_id = _player_id;
        loc=3;
      }
      // close the file:
      myFile.close();

  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening nwconfig.txt");
  }
}
short int max_fifo(short int _acc_fifo[])
{
  short int x = 0,i;
  x = _acc_fifo[0];
  for(i=0;i<FIFO_SIZE-1;i++)
  {
    if(x <= _acc_fifo[i+1]) x = _acc_fifo[i+1];
  }
  return x;

}


short int max_acc_xyz(short int _buff_acc[])
{
  short int x;
  x = max(abs( _buff_acc[0]),abs( _buff_acc[1]));
  x = max(x,abs( _buff_acc[2]));
  return x;
}


void read_acc(byte dev_add, short int _buff_acc[]) {
  byte _buff[6];
  readFrom( 0x32, 0x06, _buff, dev_add); //read the acceleration data from the ADXL375
  _buff_acc[0] = (((short int)_buff[1]) << 8) | _buff[0];
  _buff_acc[1] = (((short int)_buff[3]) << 8) | _buff[2];
  _buff_acc[2] = (((short int)_buff[5]) << 8) | _buff[4];
}




void init_acc(byte dev_add)
{
writeTo(0x31, 0x01, dev_add);
delay(2);
writeTo(0x2D, 0x08, dev_add);
}




void write_rawdata_sd(short int _buff_acc_1[],short int _buff_acc_2[],short int _b_temp, short int _h_rate)
{
  String rawString = "";
  rawString = String(_buff_acc_1[0])+ " " +String(_buff_acc_1[1])+ " " +String(_buff_acc_1[2])+ " " +String(_buff_acc_2[0])+ " " +String(_buff_acc_2[1])+ " " +String(_buff_acc_2[2])+ " " +String(_b_temp)+ " " +String(_h_rate);
  File myFile = SD.open(log_file, FILE_WRITE);
  if (myFile) {
    myFile.println(rawString);
    myFile.close();
    // print to the serial port too:
    //Serial.println(rawString);
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening logfile.txt");
  }
}


void write_marker_sd(void)
{
  File myFile = SD.open(log_file, FILE_WRITE);
  if (myFile) {
    myFile.println("acc1x acc1y acc1z acc2x acc2y acc2z bodt_temp heart_rate  ");
    myFile.close();
    // print to the serial port too:
    //Serial.println(log_file);
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening logfile.txt");
  }
}
void send_packet_udp(short int _p,short int _q,short int _r,short int _s,short int _t)
{
    String str="";
    Udp.beginPacket(broadcast_ip, 2390);
    str=String((uint8_t)_p); //converting integer into a string
    str.toCharArray(ReplyBuffer,5);
    //Udp.write(" ");
    Udp.write(ReplyBuffer);
    str=String((uint8_t)_q); //converting integer into a string
    str.toCharArray(ReplyBuffer,5);
    Udp.write(" ");
    Udp.write(ReplyBuffer);
    str=String((uint8_t)_r); //converting integer into a string
    str.toCharArray(ReplyBuffer,5);
    Udp.write(" ");
    Udp.write(ReplyBuffer);
    str=String((uint8_t)_s); //converting integer into a string
    str.toCharArray(ReplyBuffer,5);
    Udp.write(" ");
    Udp.write(ReplyBuffer);
    str=String((uint8_t)_t); //converting integer into a string
    str.toCharArray(ReplyBuffer,5);
    Udp.write(" ");
    Udp.write(ReplyBuffer);
    Udp.write(" ");
    Udp.endPacket();
}




void writeTo(byte address, byte val, byte dev_add) {
  Wire.beginTransmission(dev_add); // start transmission to device
  Wire.write(address);            // send register address
  Wire.write(val);                // send value to write
  Wire.endTransmission();        // end transmission
}


// Reads num bytes starting from address register on device in to _buff array
void readFrom(byte address, int num, byte _buff[], uint16_t dev_add) {
  Wire.beginTransmission(dev_add); // start transmission to device
  Wire.write(address);            // sends address to read from
  Wire.endTransmission();        // end transmission


  Wire.beginTransmission(dev_add); // start transmission to device
  Wire.requestFrom(dev_add, num);    // request 6 bytes from device


  int i = 0;
  while(Wire.available())        // device may send less than requested (abnormal)
  {
    _buff[i] = Wire.read();    // receive a byte
    i++;
  }
  Wire.endTransmission();        // end transmission
}
uint8_t read_temp(void)
{
    int tempData = readRegister(0x50, 2); // 0x50 is read commad for 0x02 register
    tempData = tempData/8;// MSB bit15 and LSB bit4 so received value need to be divide/8
    // convert the temperature to celsius and display it:
    float realTemp = (float)tempData * 0.0625;
    return (uint8_t) realTemp;
}


//Read from register from the ADT7320:
unsigned int readRegister(byte thisRegister, int bytesToRead ) {
  byte inByte = 0;          // incoming byte from the SPI
  unsigned int result = 0;  // result to return
  digitalWrite(ss, LOW);  // take the chip select low to select the device:
  SPI_transfer(thisRegister);  // send the device the register you want to read:
  result = SPI_transfer(0xFF);  // send a value of 0 to read the first byte returned:
  bytesToRead--;  // decrement the number of bytes left to read:
  if (bytesToRead > 0) {
    result = result << 8;
    inByte = SPI_transfer(0xFF);
    result = result | inByte;
    bytesToRead--;
  }
  digitalWrite(ss, HIGH);
  return(result);
}


//Sends a write command to ADT7320
void writeRegister(byte thisRegister, byte thisValue) {
  digitalWrite(ss, LOW);
  SPI_transfer(thisRegister); //Send register location
  SPI_transfer(thisValue);  //Send value to record into register
  digitalWrite(ss, HIGH);
}


unsigned char SPI_transfer(unsigned char byt)
{
unsigned char counter;
for(counter = 8; counter; counter--)
{
if (byt & 0x80)
digitalWrite(mosi, HIGH);
else
digitalWrite(mosi, LOW);
digitalWrite(sck, LOW);
byt <<= 1;
if (digitalRead(miso))
byt |= 0x01;
digitalWrite(sck, HIGH);
}
return(byt);
}


void init_ADT7320(void)
{
  pinMode(sck, OUTPUT);
  pinMode(mosi, OUTPUT);
  pinMode(ss, OUTPUT);
  pinMode(miso, INPUT);
  digitalWrite(sck, HIGH);
  digitalWrite(ss, HIGH);
}
void hr_isr()
{
  short int hr;
  hr = millis() - pre_millis;
  pre_millis = millis();
  hr = 60000/hr;
  if(hr > 45 && hr < 180)
  heart_rate = (heart_rate*14 + hr)/15;
  //Serial.println(heart_rate);
}


void set_logfile_name(void)
{
  DateTime now = rtc.now();
  sprintf(log_dir,"pid%1d/%04d%02d%02d",player_id,now.year(),now.month(),now.day());
  sprintf(log_file,"pid%1d/%04d%02d%02d/%02d%02d%02d.txt",player_id,now.year(),now.month(),now.day(),now.hour(),now.minute(),now.second());
  Serial.println(log_file);
  SD.mkdir(log_dir);
}


void check_wifi_status(void)
{
  if ( WiFi.status() != WL_CONNECTED)
    {
      digitalWrite(LED2_NW,HIGH);
      delay(30);
      digitalWrite(LED2_NW,LOW);
      delay(300);
    }
}


void check_update_uart_data(void)
{
  if(Serial.available()>7)
  {
    while(Serial.read() != 0x01);
    _buff_acc1[0]=Serial.read();
    _buff_acc1[1]=Serial.read();
    _buff_acc1[2]=Serial.read();
  }
}






 

2.2 MSP430 Slave mcu Source Code: Source compiled with energia-0101E0015


/*
  Sudden Impact Wearable Design Challenge
  Sponsered By element14 , Analog Devices, Tektronix and Electrolube
  Special Thanks to Texas Instruments for providing CC3200MOD, MSP430, TLV3402 Samples free of cost

  Source code for Slave MCU (MSP430G2553)
  Interfaces :
  1. Two ADXL377 interface via A3,A4,A5
  2. SUB GHz TRx interfaced via UART

  Source compiled with energia-0101E0015
  Target MCU : TI MSP430G2553

  By: Ravi Butani
  Date:29th April,2015
  e-mail: ravi_butani@yahoo.com
  Source Code is Released Under Creative Commons Attribution-ShareAlike 3.0 Unported Licence

*/
#define fifo_size 30
#define send_count 8
int8_t fifo_point=0;
int8_t send_point=0;
int8_t accx[fifo_size],accy[fifo_size],accz[fifo_size];
int8_t max_accx,max_accy,max_accz;
// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600); // msp430g2231 must use 4800
}


// the loop routine runs over and over again forever:
void loop() {
  int8_t count;
  // read the input on analog pin A3:
  accx[fifo_point] = (analogRead(A3)-510);
  accy[fifo_point] = (analogRead(A4)-510);
  accz[fifo_point] = (analogRead(A5)-510);

  fifo_point=fifo_point+1;
  if(fifo_point == fifo_size){fifo_point=0;}
  max_accx = abs(accx[0]);
  max_accy = abs(accy[0]);
  max_accz = abs(accz[0]);
  for(count=0;count<fifo_size-1; count++)
  {
    if(max_accx <= abs(accx[count+1])) max_accx = abs(accx[count+1]);
    if(max_accy <= abs(accy[count+1])) max_accy = abs(accy[count+1]);
    if(max_accz <= abs(accz[count+1])) max_accz = abs(accz[count+1]);
  }
  // print out the value you read:
  send_point=send_point+1;
  if(send_point == send_count)
  {
  send_point = 0;
  Serial.write(0x01);
  Serial.write(max_accx+1);
  Serial.write(max_accy+1);
  Serial.write(max_accz+1);
  //Serial.println(" ");
  }
  delay(12); // delay in between reads for stability
}






 

Also I am attaching my entire energia Sketchbook here so You can just change Sketchbook location for energia and start the development where I have done so far...

 

Your Valuable feedback/suggestion are welcome here...

 

Thanks,

 

Ravi