使用S4A连调Arduino控制器

 时间:2024-10-12 05:36:23

1、百度直接搜索 S4A,第一个就是 S4A 的官方网站,进入即可免费下载软件

使用S4A连调Arduino控制器

2、下载后软件如图所示,解压后,直接进行安装即可注:软件可以支持在不同的操作系统下运行,这里以 windows 为例,大家可根据操作系统选择相应版本的软件

使用S4A连调Arduino控制器

3、安装后,双击启动软件,启动效果如下图所示

使用S4A连调Arduino控制器

4、将 Arduino UNO 控制器连接到电脑,通过电脑的设备管理器可以查看到控制器的端口,记住这个端口号,一会儿连接时需要使用

使用S4A连调Arduino控制器

5、打开 Arduino IDE 软件,上传琶敷佤庐连接 S4A 的引导程序代码:#define TIMER2_PRELOAD 100ch锾攒揉敫ar outputs[10];int states[10];unsigned long initialPulseTime;unsigned long lastDataReceivedTime;volatile boolean updateServoMotors;volatile boolean newInterruption;void setup(){Serial.begin(38400);Serial.flush();configurePins();configureServomotors();lastDataReceivedTime = millis();}void loop(){if (updateServoMotors){ sendUpdateServomotors(); sendSensorValues(); updateServoMotors = false;}else{ readSerialPort();}}void configurePins(){for (int index = 0; index < 10; index++){ states[index] = 0; pinMode(index+4, OUTPUT); digitalWrite(index+4, LOW); //reset pins}pinMode(14,INPUT);pinMode(15,INPUT);outputs[0] = 'c'; //pin 4outputs[1] = 'a'; //pin 5outputs[2] = 'a'; //pin 6outputs[3] = 'c'; //pin 7outputs[4] = 's'; //pin 8outputs[5] = 'a'; //pin 9outputs[6] = 'd'; //pin 10outputs[7] = 'd'; //pin 11outputs[8] = 'd'; //pin 12outputs[9] = 'd'; //pin 13}void configureServomotors() //servomotors interruption configuration (interruption each 10 ms on timer2){newInterruption = false;updateServoMotors = false;TCCR2A = 0;TCCR2B = 1<<CS22 | 1<<CS21 | 1<<CS20;TIMSK2 = 1<<TOIE2; //timer2 Overflow InterruptTCNT2 = TIMER2_PRELOAD; //start timer}void sendSensorValues(){ int sensorValues[6], readings[5], sensorIndex; //for (sensorIndex = 0; sensorIndex < 6; sensorIndex++) //for analog sensors, calculate the median of 5 sensor readings in order to avoid variability and power surges for (sensorIndex = 2; sensorIndex < 6; sensorIndex++) //for analog sensors, calculate the median of 5 sensor readings in order to avoid variability and power surges { for (int p = 0; p < 5; p++) readings[p] = analogRead(sensorIndex); InsertionSort(readings, 5); //sort readings sensorValues[sensorIndex] = readings[2]; //select median reading } //send analog sensor values //for (sensorIndex = 0; sensorIndex < 6; sensorIndex++) for (sensorIndex = 2; sensorIndex < 6; sensorIndex++) ScratchBoardSensorReport(sensorIndex, sensorValues[sensorIndex]); //send digital sensor values //ScratchBoardSensorReport(6, digitalRead(2)?1023:0); //将数字接口2、3改为14、15 //ScratchBoardSensorReport(7, digitalRead(3)?1023:0); ScratchBoardSensorReport(6, digitalRead(14)?1023:0); ScratchBoardSensorReport(7, digitalRead(15)?1023:0);}void InsertionSort(int* array, int n){ for (int i = 1; i < n; i++) for (int j = i; (j > 0) && ( array[j] < array[j-1] ); j--) swap( array, j, j-1 );}void swap (int* array, int a, int b){ int temp = array[a]; array[a] = array[b]; array[b] = temp;}void ScratchBoardSensorReport(int sensor, int value) //PicoBoard protocol, 2 bytes per sensor{ Serial.write( B10000000 | ((sensor & B1111)<<3) | ((value>>7) & B111)); Serial.write( value & B1111111);}void readSerialPort(){ int pin, inByte, sensorHighByte; if (Serial.available() > 1) { lastDataReceivedTime = millis(); inByte = Serial.read(); if (inByte >= 128) // Are we receiving the word's header? { sensorHighByte = inByte; pin = ((inByte >> 3) & 0x0F); while (!Serial.available()); // Wait for the end of the word with data inByte = Serial.read(); if (inByte <= 127) // This prevents Linux ttyACM driver to fail { states[pin - 4] = ((sensorHighByte & 0x07) << 7) | (inByte & 0x7F); updateActuator(pin - 4); } } } else checkScratchDisconnection();}void reset() //with xbee module, we need to simulate the setup execution that occurs when a usb connection is opened or closed without this module{ for (int pos = 0; pos < 10; pos++) //stop all actuators { states[pos] = 0; digitalWrite(pos + 2, LOW); } //reset servomotors newInterruption = false; updateServoMotors = false; TCNT2 = TIMER2_PRELOAD; //protocol handshaking sendSensorValues(); lastDataReceivedTime = millis();}void updateActuator(int pinNumber){ if (outputs[pinNumber] == 'd') { digitalWrite(pinNumber + 4, states[pinNumber]); } else if (outputs[pinNumber] == 'a') analogWrite(pinNumber + 4, states[pinNumber]);}void sendUpdateServomotors(){ for (int p = 0; p < 10; p++) { if (outputs[p] == 'c') //360度连续旋转舵机 由4,7改为12,13 { if (p == 0) { servomotorC(11, states[p]); } if(p == 3) { servomotorC(12, states[p]); } } //if (outputs[p] == 'c') servomotorC(p + 4, states[p]); //if (outputs[p] == 's') servomotorS(p + 4, states[p]); if (outputs[p] == 's') servomotorS(10, states[p]); //舵机驱动,数字接口8改为数字接口10 }}void servomotorC (int pinNumber, int dir){ if (dir == 1) pulse(pinNumber, 1300); //clockwise rotation else if (dir == 2) pulse(pinNumber, 1700); //anticlockwise rotation}void servomotorS (int pinNumber, int angle){ if (angle < 0) pulse(pinNumber, 600); else if (angle > 180) pulse(pinNumber, 2400); else pulse(pinNumber, (angle * 10) + 600);}void pulse (int pinNumber, int pulseWidth){ initialPulseTime = micros(); digitalWrite(pinNumber, HIGH); while (micros() < pulseWidth + initialPulseTime){} digitalWrite(pinNumber, LOW);}void checkScratchDisconnection() //the reset is necessary when using an wireless arduino board (because we need to ensure that arduino isn't waiting the actuators state from Scratch) or when scratch isn't sending information (because is how serial port close is detected){ if (millis() - lastDataReceivedTime > 1000) reset(); //reset state if actuators reception timeout = one second}ISR(TIMER2_OVF_vect) //timer1 overflow interrupt vector handler{ //timer2 => 8 bits counter => 256 clock ticks //preeescaler = 1024 => this routine is called 61 (16.000.000/256/1024) times per second approximately => interruption period = 1 / 16.000.000/256/1024 = 16,384 ms //as we need a 20 ms interruption period but timer2 doesn't have a suitable preescaler for this, we program the timer with a 10 ms interruption period and we consider an interruption every 2 times this routine is called. //to have a 10 ms interruption period, timer2 counter must overflow after 156 clock ticks => interruption period = 1 / 16.000.000/156/1024 = 9,984 ms => counter initial value (TCNT) = 100 if (newInterruption) { updateServoMotors = true; } newInterruption = !newInterruption; TCNT2 = TIMER2_PRELOAD; //reset timer}

使用S4A连调Arduino控制器

6、下载成功后,转到 S4A 界面,首先关闭软件的自动搜索控制器,在面板上单击右键,操作如图所示

使用S4A连调Arduino控制器

7、再次单击右键,选择要连接的端口,这里控制器的端口是 com4,选择相应的端口号即可

使用S4A连调Arduino控制器
使用S4A连调Arduino控制器

8、连接成功后,数据面板会实时显示出检测到的数据,并且会显示当前连接的端口

使用S4A连调Arduino控制器

9、如果要断开控制器的连接,可以直接在面板上单击右键,然后选择关闭端口

使用S4A连调Arduino控制器
使用S4A连调Arduino控制器
  • 我的世界1.7.10工业2教程
  • 天天躲猫猫第三十四关过关攻略
  • 剑三怎么赚钱
  • 我的世界豹猫用什么食物驯服
  • 我的世界汽车怎么造
  • 热门搜索
    手抄报国庆节 爱护牙齿手抄报 手抄报花边简笔画 我和我的祖国手抄报简单又漂亮 手抄报图片大全3年级 新学期新气象手抄报内容 读书手抄报资料 大自然的手抄报 元旦手抄报图片大全 读书日手抄报