chipKIT® Development Platform

Inspired by Arduino™

how to use newsoftserial with max32.

Created Sat, 16 Jul 2011 03:09:48 +0000 by alphamike


alphamike

Sat, 16 Jul 2011 03:09:48 +0000

I have read the other posts of how to use a lib but I keep getting errors saying the compiler can not find the lib "???".h file. I have tried creating a lib folder in the sketch folder but that did not work either. I also tried putting newsoftserial.h in the same folder as my program sketch. or is there an easier way to use the built in serial lines? :?:


ron.dunn

Sat, 16 Jul 2011 03:24:15 +0000

I don't think that NewSoftSerial has been ported to Max32 yet. Happy to be corrected on that.

Instead, use the devices Serial, Serial1, Serial2 and Serial3. These are HardwareSerial instances.

They are the block of pins at the top right of the board, labelled "Communication". There is a TX and RX pin for each serial device.


alphamike

Sat, 16 Jul 2011 04:40:18 +0000

This is a clip from my code, how would i convert this to the hardware serial.

#include <SoftwareSerial.h>  
#define rxPin 3  // pin 3 connects to SMC TX  (not used in this example)
#define txPin 8  // pin 4 connects to SMC RX  
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);


/**************************************************************
 * Subroutine to exit saft start;
 ***************************************************************/
void exitSafeStartBoth()  
{  
  mySerial.print(0x83, BYTE);  
}  
/**************************************************************
 * Subroutine to control right motor= motorRight(speed, direction);
 ***************************************************************/
void setMotorSpeedleft(int speedleft)
{
  if (speedleft < 0)
  {
    mySerial.print(0xAA, BYTE);
    mySerial.print(0xd, BYTE);
    mySerial.print(0x6, BYTE); // motor reverse command
    speedleft = -speedleft; // make speed positive
  }
  else
  {
    mySerial.print(0xAA, BYTE);
    mySerial.print(0xd, BYTE);  
    mySerial.print(0x5, BYTE); // motor forward command
  }
  mySerial.print((unsigned char)(speedleft & 0x1F), BYTE);
  mySerial.print((unsigned char)(speedleft >> 5), BYTE);
}

/**************************************************************
 * Subroutine to control left motor
 ***************************************************************/
void setMotorSpeedright(int speedright)
{
  if (speedright < 0)
  {
    mySerial.print(0xAA, BYTE);
    mySerial.print(0xa, BYTE);
    mySerial.print(0x06, BYTE); // motor reverse command
    speedright = -speedright; // make speed positive
  }
  else
  {
    mySerial.print(0xAA, BYTE);
    mySerial.print(0xa, BYTE);  
    mySerial.print(0x05, BYTE); // motor forward command
  }
  mySerial.print((unsigned char)(speedright & 0x1F), BYTE);
  mySerial.print((unsigned char)(speedright >> 5), BYTE);
}

ron.dunn

Sat, 16 Jul 2011 04:47:19 +0000

Quite easy, I think.

I don't have your code/device to test, but I think the following method should work.

  1. Pick a port. Let's use Serial3.

  2. Delete the first four lines, from #include to SoftwareSerial.

  3. Insert this line in your code:

HardwareSerial mySerial = Serial3;

  1. Add the following line in your Setup() routine (or elsewhere if you choose):

mySerial.begin (9600);

  1. Connect your wires to the appropriate TX and RX pins in the Communications section of the board, as described in a previous post.

Your code should now work :)


alphamike

Sat, 16 Jul 2011 04:50:06 +0000

Is it as simple as rewriting the code like this

if (speedleft < 0)
  {
    mySerial.print(0xAA, BYTE);
    mySerial.print(0xd, BYTE);
    mySerial.print(0x6, BYTE); // motor reverse command
    speedleft = -speedleft; // make speed positive
  }

To

if (speedleft < 0)
  {
    Serial.1.print(0xAA, BYTE);
   Serial.1.print(0xd, BYTE);
    Serial.1.print(0x6, BYTE); // motor reverse command
    speedleft = -speedleft; // make speed positive
  }

but how would I define the tx and rx? or is that all based on what serial port I use. Like serial.1 is predefined as 17 = tx and 18 = rx??????? :?:


alphamike

Sat, 16 Jul 2011 04:53:30 +0000

Hmmm, I think I see what you are saying. by using "HardwareSerial mySerial = Serial3;" this keeps me from having the rewrite each serial print line.
My whole code looks bad but i'm very new to this and i'm really trying to get all this stuff to work together. I've got an RC controller and two serial motor controllers. and i'm having a lot of issues trying to get the max32 to consistently read the RC receiver. I tried using interrupts but I don't think they work either.

unsigned long counter = 0;
unsigned long Channel1Value;
unsigned long Channel2Value;
unsigned long lastgood1;
unsigned long lastgood2;
unsigned long InitialSteer;
unsigned long InitialThrottle;
unsigned long InitialMotorLimit;
long MotorLimit;
int Channel3Value;
boolean Right;
//motor stuff
int Steer;
int Thrust;
int RightMotor;
int LeftMotor;
int Chan1 = 11;
int Chan2 = 10;
int Chan3 = 9;


HardwareSerial mySerial = Serial3;

/**************************************************************
 * Subroutine to exit saft start;
 ***************************************************************/
void exitSafeStartBoth()  
{  
  mySerial.print(0x83, BYTE);  
}  
/**************************************************************
 * Subroutine to control right motor= motorRight(speed, direction);
 ***************************************************************/
void setMotorSpeedleft(int speedleft)
{
  if (speedleft < 0)
  {
    mySerial.print(0xAA, BYTE);
    mySerial.print(0xd, BYTE);
    mySerial.print(0x6, BYTE); // motor reverse command
    speedleft = -speedleft; // make speed positive
  }
  else
  {
    mySerial.print(0xAA, BYTE);
    mySerial.print(0xd, BYTE);  
    mySerial.print(0x5, BYTE); // motor forward command
  }
  mySerial.print((unsigned char)(speedleft & 0x1F), BYTE);
  mySerial.print((unsigned char)(speedleft >> 5), BYTE);
}

/**************************************************************
 * Subroutine to control left motor
 ***************************************************************/
void setMotorSpeedright(int speedright)
{
  if (speedright < 0)
  {
    mySerial.print(0xAA, BYTE);
    mySerial.print(0xa, BYTE);
    mySerial.print(0x06, BYTE); // motor reverse command
    speedright = -speedright; // make speed positive
  }
  else
  {
    mySerial.print(0xAA, BYTE);
    mySerial.print(0xa, BYTE);  
    mySerial.print(0x05, BYTE); // motor forward command
  }
  mySerial.print((unsigned char)(speedright & 0x1F), BYTE);
  mySerial.print((unsigned char)(speedright >> 5), BYTE);
}



void setup()
{
  mySerial.begin(19200);
  Serial.println("Ready");
  pinMode (Chan3, INPUT); // connect Rx channel 1 to PD3, which is labled "D3" on the Arduino board
  pinMode (Chan2, INPUT); // connect Rx channel 2 to PD4, which is labled "D4" on the Arduino board
  pinMode (Chan1, INPUT); //  connect Rx channel 3 to PD5, which is labled "D5" on the Arduino board
  InitialSteer = (pulseIn (Chan2, HIGH)/100); //read RC channel 1
  lastgood1 = InitialSteer;
  InitialThrottle = (pulseIn (Chan1, HIGH)/100); //read RC channel 2
  lastgood2 = InitialThrottle; 
  InitialMotorLimit = (pulseIn (Chan3, HIGH)/100); 
}

void loop()
{
  //  counter++;  // this just increments a counter for benchmarking the impact of the pulseIn's on CPU performance
  Channel1Value = (pulseIn (Chan1, HIGH, 50000)/100); //read RC channel 1
  Channel2Value = (pulseIn (Chan2, HIGH, 35000)/100); //read RC channel 2
  Channel3Value = (pulseIn (Chan3, HIGH, 50000)/100); // read RC channel 3


  if (Channel1Value == 0) {
    Channel1Value = lastgood1;
  } 
  else {
    lastgood1 = Channel1Value;
  }
  if (Channel1Value < InitialSteer) {
    Right = false;
  } 
  else {
    Right = true;
  }

  if (Right) {
    Steer = Channel1Value - InitialSteer;
  } 
  else {
    // Steer = InitialSteer - Channel1Value;
  }
  Steer = map(Channel1Value, 34, 56, -(MotorLimit), MotorLimit); // convert to -3200 to 3200
  constrain (Steer, -MotorLimit, MotorLimit); //just in case

  // Thrust = Channel2Value - InitialThrottle;
  Thrust = map(Channel2Value, 36, 55, -(MotorLimit), MotorLimit); // convert to 0-100 range
  constrain (Thrust, -(MotorLimit), MotorLimit); //just in case
  MotorLimit = map(Channel3Value, 35, 54, 0, 3200); // map to 0-3200
  constrain (MotorLimit, 0, 3200);

  Serial.print ("Channel 1: ");  // if you turn on your serial monitor you can see the readings.
  Serial.println (Channel1Value);
  Serial.print("Channel 2: ");
  Serial.println (Channel2Value);
  Serial.print ("Channel 3: ");
  Serial.println (Channel3Value);
  Serial.println(LeftMotor);
  Serial.println(RightMotor);
  Serial.println("");
  Serial.println("");
  Serial.println("");
  Serial.println("");

  if (Right == true) // turn right 
  {
    RightMotor = Thrust - Steer; // reduce power to the motor in the direction you want to go 
    // if (RightMotor < 0) RightMotor = 0; //mike changed this so that they will run backwards
    LeftMotor = Thrust + Steer; // increase power to the motor opposite the direction you want to go
    if (LeftMotor > 3200) LeftMotor = 3200;
  }
  if (Right == false) //turn left
  {
    LeftMotor = Thrust - Steer; // reduce power to the motor in the direction you want to go 
    // if (LeftMotor < 0) LeftMotor = 0; //mike changed this so that they will run backwards
    RightMotor = Thrust + Steer; // increase power to the motor opposite the direction you want to go
    if (RightMotor > 3200) RightMotor = 3200;

  }
  constrain (LeftMotor, -3200, 3200);
  constrain (RightMotor, -3200, 3200);
  setMotorSpeedleft(LeftMotor);
  setMotorSpeedright(RightMotor);
  delay(500);
}

ron.dunn

Sat, 16 Jul 2011 04:56:08 +0000

I think we posted across the top of each other :)

It isn't "Serial.1", it is "Serial1". Or 2. Or 3.

The following pins apply to each port:

Serial3:    Tx=14    Rx=15
Serial2:    Tx=16    Rx=17
Serial1:    Tx=18    Rx=19

alphamike

Sat, 16 Jul 2011 05:06:10 +0000

Okay i'll try that out and I hope it works. thanks. I had most of this working on my old Arduino duemilanove but that board shorted out and I decided to upgrade.