I am working on a big project which will have many servos. As a result I've acquired a serial servo controller. I am trying to write a class which will encapsulate the code required to work with this controller and I've hit a roadblock that seems to be rooted in my C++ skill (or lack thereof). The controller is capable of managing 6 or 7 servos so I've written a class of which I can construct instances as I add servos.
At this point I have one error which is causing a reboot. I assume it is something to do with the way I am encapsulating and instantiating the NewSoftSerial library because when I call _serial->print the chip reboots! I've marked this line with a few comments.
Any advice as to the structure of things is welcome but I'd sure like to sort out what I'm doing wrong first.
Thanks for looking.
I would like the end result to look something like this:
SerialServo arm;
SerialServo head;
void setup(){
SerialServo::init();
//zero and one are the positions on the servo controller
arm.attach(0);
head.attach(1);
}
void loop(){
//.......do stuff
head.set(120);
arm.set(36);
//.......do more stuff
}
And here is what I have so far:
//main arduino sketch
#include <NewSoftSerial.h>
#include <Math.h>
#include "SerialServo.h"
SerialServo servo;
void setup() {
Serial.begin(9600);
Serial.println("Awake!");
SerialServo::init(7,8);
Serial.println("post init");
servo.set(150);
}
void loop () {}
//the serialservo.h file
#ifndef SERIALSERVO_H
#define SERIALSERVO_H
#include "NewSoftSerial.h"
class SerialServo {
public:
// rx, tx
static NewSoftSerial *_serial;//(9, 8);
int _servo_id;
static void init(char tx, char rx);
static void send(int servo_id, int angle);
void attach(int servo_id);
void set(int spot);
};
#endif
//the serialservo.cpp file
#ifndef SERIALSERVO_CPP
#define SERIALSERVO_CPP
#include "WProgram.h"
#include "SerialServo.h"
//static
void SerialServo::init(char tx, char rx){
_serial = &NewSoftSerial(rx, tx);
_serial->begin(9600);
Serial.println("SerialServo::init");
}
//static
void SerialServo::send(int servo_id, int angle){
unsigned char buff[6];
int temp = angle & 0x1f80;
char pos_hi = temp >> 7;
char pos_low = angle & 0x7f;
buff[0] = 0x80; // start byte
buff[1] = 0x01; // device id
buff[2] = 0x04; // command number
buff[3] = servo_id; // servo number
buff[4] = pos_hi; // data1
buff[5] = pos_low; // data2
for(int i=0; i<6; i++){
//
//this line causes reboot!
//
_serial->print(buff[i], BYTE);
Serial.println("SerialServo::set_angle");
}
}
void SerialServo::attach(int servo_id){
_servo_id = servo_id;
}
void SerialServo::set(int angle){
Serial.println("SerialServo::set_angle");
SerialServo::send(_servo_id, angle);
Serial.println("SerialServo::set_angle");
}
NewSoftSerial * SerialServo::_serial;
#endif
Some personal background: I've been doing arduino projects on and off since the ol' NG days. However, recently I decided I need to organize my arduino code in such a way so it can be easily understood by more than just myself. As a result I'm learning C++ classes. I'm no stranger to OOP, but C++ is a language I never had the opportunity to learn.