Welcome Guest,Register Now
Log In

ANT Forum

Welcome guest, please Login or Register

   

Setting up the AP2 module for Arduino

Rank

Total Posts: 1

Joined 2015-04-27

PM

Hi,

I am currently working on a project in which I want to implement ANT+. I bought the AP2 module from DigiKey, but now I am struggling to get communications up.

The problem is that I don't know where to start troubleshooting. The wiring? The software? De ANT Phone/Bike computer. The only thing I know is that it is not showing up on my devices.

Please take a look at the details below. I'm probably doing something stupid.

This is the module;
http://www.digikey.com/product-detail/en/ANTAP281M4IB - TRAY/1094-1003-ND/2748493

Arduino board for experimenting;
Arduino UNO

Connection between Arduino and AP2;
See added picture

Code used, example heart rate from this website;
https://github.com/jbremnant/msp430ant

#include <SoftwareSerial.h>
#include "ant_lib.h"

SoftwareSerial mySerial(23);
AntMaster am(mySerial13);  // second argument is pin number for status LED

void setup()  
{
  Serial
.begin(4800);
  
am.config_loop();
}

void loop
() // run over and over
{
  delay
(500); am.ledon();
  
am.sendHRM(90);  // send your data here
  
delay(500); am.ledoff();


#include <SoftwareSerial.h>
#include "Arduino.h"
#include "ant_lib.h"

// we pass by reference, but copies.. sigh.
AntMaster::AntMaster(SoftwareSerialmySerialuint8_t ledPin) :
  
_swuart(mySerial),
  
_ledPin(ledPin)
{
  _swuart
.begin(4800);    // ant+ breakout is set at 4800 baud rate by default
  
pinMode(_ledPinOUTPUT);
}
  
void AntMaster
::txMessage(uint8_tmessageuint8_t  messageSize)
{
  uint8_t i
;

  
txBufferPos   0;                       // set position to 0
  
txBufferSize  messageSize 3;         // message plus syc, size and checksum
  
txBuffer[0]   0xa4;                    // sync byte
  
txBuffer[1]   = (uint8_tmessageSize 1// message size - command size (1)

  
for(i=0i<messageSizei++)
    
txBuffer[2+i] message[i];

  
// calculate the checksum
  
for(i=0i<txBufferSize 1; ++i)
    
txBuffer[txBufferSize 1] txBuffer[txBufferSize 1] txBuffer[i];

  
  
// now send via UART
  
for(i=0i<txBufferSizei++)
  
{
    _swuart
.write(txBuffer[i]);
    
Serial.print(txBuffer[i]HEX);
    
Serial.print(' ');
  
}
  Serial
.print('\n');
}


// Resets module
void AntMaster::reset()
{
  uint8_t msgbuf[2]
;
  
msgbuf[0] 0x4a// ID Byte
  
msgbuf[1] 0x00// Data Byte N (N=LENGTH)
  
txMessage(msgbuf2);
}

// Assigns CH=0, CH Type=10(TX), Net#=0
void AntMaster::assignch()
{
  uint8_t msgbuf[4]
;
  
msgbuf[0] 0x42;
  
msgbuf[1] ANT_CH_ID;    // Channel ID, 0x00 for HRM, 0x01 for custom
  
msgbuf[2] ANT_CH_TYPE;  // CH Type
  
msgbuf[3] ANT_NET_ID;   // Network ID
  
txMessage(msgbuf4);
}

// Assigns CH#, Device#=0000, Device Type ID=00, Trans Type=00
void AntMaster::setchid()
{
  uint8_t msgbuf[6]
;
  
msgbuf[0] 0x51;
  
msgbuf[1] ANT_CH_ID;      // Channel Number, 0x00 for HRM
  
msgbuf[2] ANT_DEV_ID1;    // Device Number LSB
  
msgbuf[3] ANT_DEV_ID2;    // Device Number MSB
  
msgbuf[4] ANT_DEV_TYPE;   // Device Type, 0x78 for HRM
  
msgbuf[5] ANT_TX_TYPE;
  
txMessage(msgbuf6);
}

void AntMaster
::setrf()
{
  uint8_t msgbuf[3]
;
  
msgbuf[0] 0x45;
  
msgbuf[1] = (ANT_CH_FREQ 0xFF00) >> 8;  // upper byte for RF freq
  
msgbuf[2] = (ANT_CH_FREQ 0xFF);         // lower byte for RF freq
  
txMessage(msgbuf3);
}

void AntMaster
::setchperiod()
{
  uint8_t msgbuf[3]
;
  
msgbuf[0] 0x43;
  
msgbuf[1] = (ANT_CH_PER 0xFF00) >> 8// Ch period LSB
  
msgbuf[2] = (ANT_CH_PER 0xFF);        // Ch period MSB
  
txMessage(msgbuf3);
}

// Opens CH 0
void AntMaster::opench()
{
  uint8_t msgbuf[2]
;
  
msgbuf[0] 0x4b;
  
msgbuf[1] 0x00;
  
txMessage(msgbuf2);
}


// Sends 10 bytes of data that mimics HRM data trasmission
void AntMaster::sendHRM(uint8_t num)
{
  uint8_t msgbuf[10]
;
  
msgbuf[0] 0x4e;
  
msgbuf[1] 0x00;
  
msgbuf[2] 0x00;
  
msgbuf[3] 0x00;
  
msgbuf[4] 0x00;
  
msgbuf[5] 0x00;
  
msgbuf[6] 0x00;
  
msgbuf[7] 0x00;
  
msgbuf[8] 0x00;
  
msgbuf[9] num;
  
txMessage(msgbuf10);
}

void AntMaster
::ledon(void)
{
  digitalWrite
(_ledPinHIGH);
}

void AntMaster
::ledoff(void)
{
  digitalWrite
(_ledPinLOW);
}

void AntMaster
::config(uint8_t msec)
{
  ledon
();

  
reset();       delay(msec);
  
assignch();    delay(msec);
  
setrf();       delay(msec);
  
setchperiod(); delay(msec);
  
setchid();     delay(msec);
  
opench();      delay(msec);

  
ledoff();
}

void AntMaster
::config_loop()
{
  uint8_t i
;

  for(
i=1i<=20i++)
  
{
    config
(5*i);
  
}


#ifndef _AntMaster_h
#define _AntMaster_h

#include <inttypes.h>
#include <SoftwareSerial.h>

#define ANT_CH_ID    0x00
#define ANT_CH_TYPE  0x10
#define ANT_NET_ID   0x00
#define ANT_DEV_ID1  0x00
#define ANT_DEV_ID2  0x00
#define ANT_DEV_TYPE 0x00
#define ANT_TX_TYPE  0x00
#define ANT_CH_FREQ  0x0039
#define ANT_CH_PER   0x1f86

// this PD4 pin on atmega168 maps to arduino's digital pin 4
// #define LEDBIT PD4

class AntMaster 
{
public:
  
AntMaster(SoftwareSerialmySerialuint8_t ledPin 4);
  
void reset(void);
  
void assignch(void); 
  
void setrf(void); 
  
void setchperiod(void);
  
void setchid(void);  
  
void opench(void);   
  
void sendHRM(uint8_t num);
  
void config(uint8_t msec);
  
void config_loop();
  
void ledon();
  
void ledoff();
  
private:
  
SoftwareSerial _swuart;
  
uint8_t _ledPin;
  
uint8_t txBuffer[32];
  
uint8_t txBufferSize;
  
uint8_t txBufferPos;
  
void txMessage(uint8_tmessageuint8_t  messageSize);
};

#endif 

     

Image Attachments

Circuit_1.png

Click thumbnail to see full-size image